Skip to content

Commit

Permalink
Fix issue with native lazyloading not recognized (#1316)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Brencz <[email protected]>
  • Loading branch information
gmetais and macbre authored Sep 18, 2023
1 parent 9e8cf73 commit 5e2f929
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion modules/lazyLoadableImages/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
len = images.length,
offset,
path,
native,
processedImages = {},
src,
viewportHeight = window.innerHeight;
Expand All @@ -30,6 +31,9 @@
// @see https://stackoverflow.com/questions/35586728/detect-used-srcset-or-picture-tag-source-with-javascript
src = images[i].currentSrc;

// Chrome headless loads images with native lazyloading, therefore we need to filter by ourself.
native = images[i].loading === "lazy";

// ignore base64-encoded images
if (src === null || src === "" || /^data:/.test(src)) {
continue;
Expand All @@ -42,6 +46,7 @@
processedImages[src] = {
offset: offset,
path: path,
native: native,
};
}

Expand All @@ -50,6 +55,7 @@
processedImages[src] = {
offset: offset,
path: path,
native: native,
};
}
}
Expand All @@ -62,7 +68,7 @@
Object.keys(processedImages).forEach((src) => {
var img = processedImages[src];

if (img.offset > viewportHeight) {
if (img.offset > viewportHeight && !img.native) {
phantomas.log(
"lazyLoadableImages: <%s> image (%s) is below the fold (at %dpx)",
src,
Expand Down
3 changes: 3 additions & 0 deletions test/webroot/lazy-loadable-images.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<article>
<h1>Issue #494</h1>
<img src="/static/mdn.png" width=250>
<img src="/static/mdn.png?nocache=1" width=25 loading="lazy">
Foo bar
</article>

Expand All @@ -44,5 +45,7 @@ <h1>Issue #494</h1>

<!-- pick the right source if a srcset is specified -->
<img src="/static/75x50.jpg" srcset="/static/75x50.jpg 75w, /static/150x100.jpg 150w" sizes="150px" width="150" height="100"/>

<img src="/static/mdn.png?nocache=2" width=25 loading="lazy"><!-- native lazyloading - ignore -->
</footer>
</body>

0 comments on commit 5e2f929

Please sign in to comment.