Skip to content

Conversation

@firestar300
Copy link
Contributor

@firestar300 firestar300 commented Dec 1, 2025

Adds lazy loading for images to improve initial page load time.

Also sets decoding="async" attribute to images with fetchpriority="high" to further enhance perceived performance.

Summary by Sourcery

Introduce image lazy loading and async decoding to improve initial page load performance.

New Features:

  • Enable lazy loading for images via the lazysizes library.

Enhancements:

  • Set async decoding on high-priority images to improve perceived rendering performance.

Build:

  • Add lazysizes as a runtime dependency in package.json.

Adds lazy loading for images to improve initial page load time.

Also sets `decoding="async"` attribute to images with `fetchpriority="high"` to further enhance perceived performance.
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 1, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduces lazysizes as a dependency and wires it into the JS entrypoint to enable lazy-loading for images, while also marking high-priority images for asynchronous decoding to improve perceived initial load performance.

Sequence diagram for image loading with lazysizes and async decoding

sequenceDiagram
    participant User
    participant Browser
    participant IndexJS
    participant Lazysizes

    User->>Browser: Navigate_to_page
    Browser->>Browser: Parse_HTML
    Browser->>Browser: Render_picture_with_img(fetchpriority=high,decoding=async)
    Browser->>Browser: Schedule_high_priority_image_download
    Browser->>Browser: Decode_image_asynchronously

    Browser->>IndexJS: Execute_JS_entrypoint
    IndexJS->>Lazysizes: Import_and_initialize_lazysizes
    Lazysizes-->>Browser: Attach_listeners_to_lazy_images

    User->>Browser: Scroll_or_interact
    Browser->>Lazysizes: Intersection_or_scroll_event
    Lazysizes->>Browser: Swap_in_real_src_for_in_view_lazy_images
    Browser->>Browser: Download_and_decode_lazy_images_on_demand
Loading

Flow diagram for handling high-priority vs lazily loaded images

flowchart TD
    Start[Image_element_in_template]
    IsHighPriority{Has_fetchpriority_high}
    HighPriority[Render_img_with_decoding_async]
    LazyPriority[Managed_by_lazysizes]
    AsyncDecode[Browser_decodes_image_asynchronously]
    LazyLoadTrigger[User_scrolls_or_image_enters_viewport]
    LoadLazy[Load_and_decode_lazy_image]
    End[Image_visible_to_user]

    Start --> IsHighPriority
    IsHighPriority -->|Yes| HighPriority
    IsHighPriority -->|No| LazyPriority

    HighPriority --> AsyncDecode --> End

    LazyPriority --> LazyLoadTrigger --> LoadLazy --> End
Loading

File-Level Changes

Change Details Files
Enable lazy loading behavior via lazysizes in the JS bundle.
  • Add lazysizes as an application dependency so it is installed and bundled.
  • Import and/or initialize lazysizes in the main JavaScript entrypoint to activate lazy loading on supported image elements.
package.json
src/js/index.js
yarn.lock
Optimize decoding behavior for non-lazy high-priority images.
  • Add the decoding="async" attribute to high-priority image tags to avoid blocking rendering during image decoding.
assets/conf-img/tpl/default-picture-nolazyload-caption.tpl
assets/conf-img/tpl/default-picture-nolazyload.tpl

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The PR description mentions adding lazy loading with lazysizes, but the diff only adds the decoding="async" attribute and the lazysizes dependency; consider either wiring lazysizes into the templates/JS or updating the description to match the actual change.
  • Double-check that decoding="async" on fetchpriority="high" images aligns with your LCP/performance goals, as async decoding on critical (above-the-fold) images can sometimes delay the first render of those images.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The PR description mentions adding lazy loading with lazysizes, but the diff only adds the `decoding="async"` attribute and the lazysizes dependency; consider either wiring lazysizes into the templates/JS or updating the description to match the actual change.
- Double-check that `decoding="async"` on `fetchpriority="high"` images aligns with your LCP/performance goals, as async decoding on critical (above-the-fold) images can sometimes delay the first render of those images.

## Individual Comments

### Comment 1
<location> `assets/conf-img/tpl/default-picture-nolazyload-caption.tpl:4` </location>
<code_context>
     %%data-location%%
     %%sources%%
-    <img %%src%% fetchpriority="high" %%attributes%%>
+    <img %%src%% fetchpriority="high" decoding="async" %%attributes%%>
 </picture>
-<figcaption>%%caption%%</figcaption>
</code_context>

<issue_to_address>
**issue (performance):** Reconsider using `decoding="async"` on images marked with `fetchpriority="high"`.

On critical, above-the-fold images, `decoding="async"` can delay when the image is decoded and painted, undermining the benefit of `fetchpriority="high"`. Consider either omitting `decoding` so the browser can choose, or using `decoding="sync"` (or making this configurable) for non-lazyloaded LCP candidates to avoid potential regressions in perceived load performance.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

%%data-location%%
%%sources%%
<img %%src%% fetchpriority="high" %%attributes%%>
<img %%src%% fetchpriority="high" decoding="async" %%attributes%%>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (performance): Reconsider using decoding="async" on images marked with fetchpriority="high".

On critical, above-the-fold images, decoding="async" can delay when the image is decoded and painted, undermining the benefit of fetchpriority="high". Consider either omitting decoding so the browser can choose, or using decoding="sync" (or making this configurable) for non-lazyloaded LCP candidates to avoid potential regressions in perceived load performance.

@herewithme
Copy link
Member

Ça fait pas doublon avec WP-Rocket ?

Je ne suis pas "pro-rocket", mais si on implément désormais nous-même la feature, il faudra couper celle du plugin ?

@firestar300
Copy link
Contributor Author

firestar300 commented Dec 1, 2025

Ça fait pas doublon avec WP-Rocket ?

Je ne suis pas "pro-rocket", mais si on implément désormais nous-même la feature, il faudra couper celle du plugin ?

Historiquement, nous utilisions lazySizes sur le BFF pour avoir une compatibilité avec advanced-responsive-images. On a échangé avec Alex pour faire évoluer le plugin et se passer de lazySizes, donc on l'a retiré du BFF mais trop tôt car la dernière version de ARI n'est pas encore compat' sans utilisation de lazySizes. J'ai eu des retours de theme dev qui souhaitent implémenter de nouveau et temporairement la lib js pour que tout fonctionne bien avec ARI.

Ça concerne uniquement les images contribuables hors zone Gutenberg (exception pour les champs ACF d'image dans les blocs sur mesure).

Mais oui sur le fond tu as raison, ça fait doublon avec WPR. Dans le futur, on aimerait ne plus utiliser lazysizes tout en conservant ARI.

@herewithme
Copy link
Member

Oui, mais du coup, on combine 2 technos de lazyload sur nos projets ? où Rocket est activé 90% du temps ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants