Vanilla-lazyload: Compatibility with loading attribute

Created on 8 Apr 2019  路  16Comments  路  Source: verlok/vanilla-lazyload

Hello,

Addy Osmani, write here https://addyosmani.com/blog/lazy-loading/ about new loading attribute.

  • Is it possible to use this module as "loading attribute" polyfill?
  • If yes, how?
  • If no, why and would it be in a future major release?
Enhancement

Most helpful comment

Hey, I've read Addy's article till the end, and YES, I think it's totally doable like he suggests, even if that solution means that your content would still rely on some JavaScript code to start loading. (so what鈥檚 the point?)

I'm going to add a new option, something like native_lazyload(*) so, when its value is true and the browser supports native lazy loading, this script will copy all the data- attributes to the proper ones.
It looks very easy to do. Will do this in version 12.

(*) big question: set the default value to true or false?

All 16 comments

Hey @ryuran,
thank you for opening this.

I was already thinking about it... I don't think it's possible, because

<img src="celebration.jpg" loading="lazy" alt="..." />
<iframe src="video-player.html" loading="lazy"></iframe>

I'm assuming that if the loading="lazy" is not supported by the browser, it would start loading all the images right away, even before the Javascript is downloaded and executed.

Anyway I'm going to read Addy's post through and see if it can be done somehow.

Thank you for the hint!

Hey, I've read Addy's article till the end, and YES, I think it's totally doable like he suggests, even if that solution means that your content would still rely on some JavaScript code to start loading. (so what鈥檚 the point?)

I'm going to add a new option, something like native_lazyload(*) so, when its value is true and the browser supports native lazy loading, this script will copy all the data- attributes to the proper ones.
It looks very easy to do. Will do this in version 12.

(*) big question: set the default value to true or false?

I lean forward true by default for the new option, since you'll check for browser support anyway

@addyosmani Could you suggest a solution here?

I guess that since the native loading feature is still under development and will need some fine tuning, not only in Chrome but in all other browsers, that default value in LazyLoad will be false. At least in the first minor releases of version 12. That will assure nothing breaks for already working websites

If you want to take a look, I'm already developing it in the feature/nativeLazyLoading branch. ;)

I've added a new demo in the master branch, already deployed and made available online on my website, in order to test native lazy loading for images. This demo doesn't require any Javascript code, it's just to try how it works.

TRY IT NOW

As far as I can see, Chrome is downloading images all the way down even if the user didn't scroll by any pixel.

Schermata 2019-04-09 alle 22 49 25

This is NOT the same goal that this script aims to pursue.

The main difference is that, using this LazyLoad script, the images outside of the viewport (plus the threshold size) are not downloaded at all.

So yes, I can continue developing this feature, but I think that in the end nobody would use it, since the final result would be very different.

What do you think?

So scripted lazy loading is images outside of the viewport are not downloaded at all.
What does native lazy loading do actually?

Not sure. @addyosmani knows better I guess. Addy?

From the spec, the native lazy loading should

Allow browsers to defer the loading of out-of-view images and iframes until the user scrolls near it.

Also to note

An implementation detail of loading in Chrome is that it fetches the first 2KB of images on page-load. It fetches the rest of the image byes when the user is about to see them. This can result in (1) double fetches to 'appear' in the DevTools Network panel and (2) Resource Timing to have 2 requests for each image.

This is used to fetch the images dimensions and prevent any content reflow.

Thank you @Tabrisrp. That鈥檚 why then. The images in that demo are very light so... they are fetched all at once. Good to know! I鈥檒l keep going with the implementation.

Just released 12.0 BETA.
Try it out on Chrome Canary!

You can install it locally via npm:

npm install [email protected]

Or include it in your page via script:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/lazyload.min.js"></script>

Hey folks. Sorry for the slow reply here. I've been silently pushing updates to my article about how our current implementation in Chrome works. We're still in the process of figuring out a number of details and loading's behavior will likely settle in the next three weeks. A few specifics:

  • We haven't yet decided on the default thresholds (i.e how near does the user need to be to an image for it to be fetched). This can impact how "many" images get fetched on page load vs lazy loaded. There's a big difference between some of the thresholds we're testing with (e.g 1px, which is what I think most folks would expect) and larger ranges that make it look like almost everything is still downloading.

  • We have a number of edge cases we need to still implement solutions for. e.g our range-request implementation (basically, that 2KB fetch for dimensions) does not yet fully take dimensions into account and skip that fetch step if they've been supplied on image. There are lots of little things like that.

I would personally label any use of loading=lazy experimental for now. We're hoping if the rest of our tests against the feature go well that we'll be able to land it for either Chrome 75 (beta in May) or Chrome 76.

Thanks @addyosmani

I've also marked version 12 of this script as beta and made the feature active only under a setting (use_native) so the users can try and experiment with native lazy-loading without, well... breaking the internet.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gerdneuman picture gerdneuman  路  8Comments

Cyriltra picture Cyriltra  路  4Comments

isuke01 picture isuke01  路  3Comments

nicomollet picture nicomollet  路  4Comments

wzhscript picture wzhscript  路  8Comments