WordPress / WordPress/performance
JS-based lazy-loaded images should be replaced with native lazy-loading
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 461
- Forks
- 165
- Avg merge
- 11h 12m
- Merged PRs (30d)
- 28
Description
See https://github.com/WordPress/performance/pull/1745:
In a future PR we could consider undoing such JS-based lazy-loading, rewriting
data-srctosrc,data-srcsetback tosrcsetand so on, and then to process theIMGas normal. This would have a dramatic improvement to LCP especially when the such JS-based lazy-loading is being done for images in the initial viewport, especially for anIMGwhich is the LCP element.
For example, Image Prioritizer should take the following from the Avada theme (code ref):
<img
src="https://example.com/foo.webp"
data-orig-src="https://example.com/foo.webp"
width="1000"
height="800"
class="lazyload wp-image-1"
srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20width%3D%271000%27%20height%3D%27800%27%20viewBox%3D%270%200%201000%20800%27%3E%3Crect%20width%3D%271000%27%20height%3D%27800%27%20fill-opacity%3D%220%22%2F%3E%3C%2Fsvg%3E"
data-srcset="https://example.com/foo-200x91.webp 200w, https://example.com/foo-300x136.webp 300w, https://example.com/foo-400x181.webp 400w, https://example.com/foo-600x272.webp 600w, https://example.com/foo-768x348.webp 768w, https://example.com/foo-800x362.webp 800w, https://example.com/foo-1024x463.webp 1024w, https://example.com/foo-1200x543.webp 1200w, https://example.com/foo-1536x695.webp 1536w, https://example.com/foo.webp 1920w"
data-sizes="auto"
data-orig-sizes="(max-width: 767px) 100vw, 1920px"
>
And transform it into:
<img
src="https://example.com/foo.webp"
width="1000"
height="800"
class="wp-image-1"
srcset="https://example.com/foo-200x91.webp 200w, https://example.com/foo-300x136.webp 300w, https://example.com/foo-400x181.webp 400w, https://example.com/foo-600x272.webp 600w, https://example.com/foo-768x348.webp 768w, https://example.com/foo-800x362.webp 800w, https://example.com/foo-1024x463.webp 1024w, https://example.com/foo-1200x543.webp 1200w, https://example.com/foo-1536x695.webp 1536w, https://example.com/foo.webp 1920w"
sizes="(max-width: 767px) 100vw, 1920px"
loading="lazy"
>
And the following from Speed Optimizer from SiteGround (code ref):
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src="https://example.com/bar.jpg" data-srcset="https://example.com/bar-large.jpg 1000w, https://example.com/bar-large.jpg 1000w" sizes="(max-width: 556px) 100vw, 556px" alt="Bar" class="attachment-large size-large wp-image-2 has-transparency lazyload" width="500" height="300">
<noscript>
<img src="https://example.com/bar.jpg" srcset="https://example.com/bar-large.jpg 1000w, https://example.com/bar-large.jpg 1000w" sizes="(max-width: 556px) 100vw, 556px" alt="Bar" class="attachment-large size-large wp-image-2 has-transparency lazyload" width="500" height="300">
</noscript>
Should be converted into:
<img src="https://example.com/bar.jpg" data-srcset="https://example.com/bar-large.jpg 1000w, https://example.com/bar-large.jpg 1000w" sizes="(max-width: 556px) 100vw, 556px" alt="Bar" class="attachment-large size-large wp-image-2 has-transparency" width="500" height="300" loading="lazy">
Then Image Prioritizer's enhancements can apply as expected, with removing loading=lazy if the IMG is in an initial viewport and also to add preloading as appropriate when it is the LCP element.
There are many implementations of JS-based lazy-loading, however, so this should prioritize what is most popular (e.g. LazySizes). On the other hand, plugins which use JS-based lazy-loading could rather be engaged with to use native lazy-loading instead.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Image Prioritizer implementation and the context from pull request 1745, then compare the LazySizes, Avada, and SiteGround markup examples in this issue. Define which JavaScript lazy-loading patterns and attributes are supported, and verify that converted images can receive the native lazy-loading and Image Prioritizer enhancements described here.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, php
- Domain
- frontend, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100