Settings

Theme

Native lazy loading has landed in Chrome

dev.to

62 points by yeasayer 6 years ago · 48 comments

Reader

sharpercoder 6 years ago

As a developer and systems architect, I really like this. As uset, I hate this so much. On slower connections, I like to load a page, swap tabs, come back later and expect the page to be loaded. This mechanism will now break (and is already brokrn by the usr of these lazy loading libs).

  • cameronbrown 6 years ago

    Hmm, maybe if websites don't use lazy loading JavaScript anymore (guilty as charged btw) then you'll be able to configure it however you want in browser settings?

    • davidweatherall 6 years ago

      I imagine anti-lazyloading extensions will start appearing soon.

      • mantas 6 years ago

        And that's exactly why common API for lazyloading is awesome. It's a PITA to write such extension when everybody is using custom approaches for lazyloading. Once there's a common API, there will be a common approach to override it.

      • rasz 6 years ago

        whole extension:

            document.querySelectorAll("img[loading]").forEach(function(e){e.loading="";})
  • bgdnyxbjx 6 years ago

    Lazy loading doesn’t have to mean waiting for user input to load a resource, or waiting for the viewport. It just gives the developer a way to break up their bundles and control over when they get loaded a fetched.

    A well designed web app would have a small start up bundle so the user can get something useful quickly, and then it would start prefetching and loading the rest of the bundles.

  • city41 6 years ago

    The article contradicts itself a little bit. It says "Currently the images are fetched with different priority in Chrome, the ones which are not in the viewport have less priority. But they're fetched as soon as possible regardless."

    But it also says: "lazy: Defer loading of resources until it reaches a calculated distance from viewport."

    If a lazy image is only loaded when within a distance of the viewport, then your "come back later" strategy stops working. But if instead off screen images are just bumped down in priority and still download after higher priority stuff finishes, then your strategy should still work.

  • tgv 6 years ago

    > As a user, ... I like to ... and expect ...

    Sorry, but the format made me laugh.

toastal 6 years ago

I'm happy to see this. So many websites with lazy loading never implemented a fallback for noscript. And most of the popular libraries didn't account for this accessibility.

  • holtalanm 6 years ago

    just curious -- in what situation would your average user not have javascript turned on?

    • rubbingalcohol 6 years ago

      It's hostile to the user to require javascript for non-app pages that don't need it.

    • SquareWheel 6 years ago

      Your average user? Almost never.

    • acdha 6 years ago

      It's becoming more common as people use aggressive blockers but the main reason to care about it is that JavaScript breaks fairly regularly. On LTE, I see pages where a simple HTML display would work but a JavaScript loader failed on first load generally a couple of times a day.

      • toastal 6 years ago

        With uMatrix I'm often without the necessary script to load basic static content. I love JavaScript and PWAs, but many sites aren't an application so I don't want extra scripts running who knows what.

tyingq 6 years ago

One watch out is that the lazy loading only grabs the first 2048 bytes. That gives you the dimensions of an image, which is a plus, but not the whole image, unless the image is tiny. That likely means more total connections. Probably fine for http/2 sites, maybe not so for non-http/2 sites. Would be interesting to see perf stats for that case.

kaycebasques 6 years ago

For anyone hearing about loading=“lazy” for the first time from this article , I’ll reiterate the main idea because the article doesn’t go into much depth about the motivation.

The main idea is that some pages use a lot of cellular data to load images that are further down the page, out of the viewport. If the user glances at the page and decides it’s not relevant and exits, then those offscreen images were just a waste of cellular data. loading=“lazy” addresses this problem by deferring the image loads until the user has scrolled the page and the image is soon to be in the viewport.

Disclosure: I write the Chrome DevTools docs

  • d--b 6 years ago

    The demo on the page clearly shows a delay between the start of the load and the actual display of the image. So "soon to be in the viewport" should be quite sooner.

Volundr 6 years ago

I may be the odd one out here, but I hate lazy loading. I get why it's a big thing on cellular connections, but I do most of my browsing on WIFI. With lazy loading I'll frequently be reading an article, reach an image that hasn't loaded in yet, and have to wait for it, even though I've been reading for several minutes. Sometimes I also have to refind my place as the whole darn page reflows.

I wish there was a middle ground... detect I'm on WIFI and go ahead and load in the lazy stuff after the above the fold stuff.

5- 6 years ago

Link could be replaced with the original from which all content was plagiarised?

https://web.dev/native-lazy-loading

outside1234 6 years ago

Edge will pick this up going forward as it is based on Chromium.

tsp 6 years ago

Seems like the image service the author is using is down because of traffic…

https://placedog.net/400/400

wereHamster 6 years ago

`if ('loading' in HTMLImageElement.prototype === true) {`

/rolleyes

  • danShumway 6 years ago

    I'm missing why this is a bad thing?

    It make polyfills way easier, and makes it drop-dead simple to programmatically toggle lazy-loading on existing elements.

    • mark_and_sweep 6 years ago

      I think the previous poster was referring to the unnecessary `=== true` part.

      • Raphmedia 6 years ago

        It's quite common to compare against "true" using "===" in JavaScript.

        • wereHamster 6 years ago

          I wouldn't say it's common. Most JavaScript developers (who aren't familiar with JS coercion rules or the difference between == and ===) wouldn't even be able to tell you for certain what the code does, exactly.

          Using === to compare against "true" makes only sense if the variable can be something other than boolean (undefined, null, string etc). Or if you are programming defensively – by choice or because the codebase is messy.

          • Raphmedia 6 years ago

            > Or if you are programming defensively – by choice or because the codebase is messy.

            This accurately describes the majority of JavaScript projects I have worked on.

            The last web agency I worked at even had linters checking against '=='.

            See this table https://dorey.github.io/JavaScript-Equality-Table/ which displays how unsure you can end up when using '=='.

            > Most JavaScript developers (who aren't familiar with JS coercion rules or the difference between == and ===)

            It rings strange to me that you would believe that developers who focus on one language ("JavaScript developers") wouldn't know the quirks of that language. Not all JS devs are juniors.

            I prefer to follow this rule, "never use == and != unless you need type coercion". You know that your expected results is 'true' then why not test for that only?

          • tobr 6 years ago

            The `in` operator always returns a boolean, though.

            • reaperducer 6 years ago

              Maybe it was just done out of habit.

              A bool can be expected today. But why about 10 years from now? Defensive programming isn't a bad thing. Making assumptions is why we had the Y2K mess.

              • mark_and_sweep 6 years ago

                That's an unreasonable, excessive use of defensive programming.

                We're not talking about an unstable API here, we're talking about an operator.

                It would be just as unreasonable to "defend" your code against `1 + 1` suddenly returning a boolean rather than a number.

                • Raphmedia 6 years ago

                  You already know that ìn` will return either `true` or `false`. Why use `==` which will convert data instead of `===` which will test for a boolean directly?

                  Ignore the symbols and use the words. Which makes more sense to you?

                  "[My test] strictly equals true." or "[My test] loosely equals true."

                • wereHamster 6 years ago

                  This looks future proof to me: `if (JSON.stringify("something" in foo).match(/t/) != null) { … }`

Dutchie2020 6 years ago

Nice, native deferred rendering for style sheets next please!

zsrxx 6 years ago

Good. I hate lazy loading. I hope they add an option in the browser so I can disable it globally.

  • josteink 6 years ago

    Chrome? Google Chrome and options?

    Hah! Good one!

    • zsrxx 6 years ago

      about:flags

      • rasz 6 years ago

        Show me on the doll where Chrome has an option to disable Ping attribute (dont bother looking for chrome://flags#disable-hyperlink-auditing, its gone), or set custom localstorage quotas?

milad_nazari 6 years ago

English isn't my first language but shouldn't it be "... has landed in Chrome" instead of "... is landed in Chrome"?

d--b 6 years ago

This is a poorly thought feature.

You always want to have the image loaded before you actually use it. You don't want to wait until you actually use the image. For instance in a carousel, you'll want to load the next and previous image, not just the current displayed one.

I'd much rather have a "loading order" than lazy loading. Sure, I don't want to load first the big images that I'll want to display later, but I definitely don't want them to appear while I scroll down...

  • acdha 6 years ago

    This is covered in the part about the “calculated distance” where they describe the logic used to load the images in the current implementation: it loads before use but waits until the images are near the viewport.

    There's an FAQ about carousels, too:

    https://web.dev/native-lazy-loading#how-does-the-loading-att...

    > How does the loading attribute work with images that are in the viewport but not immediately visible (for example, behind a carousel)? > > Only images that are below the device viewport by the calculated distance load lazily. All images above the viewport, regardless of whether they're immediately visible, load normally.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection