Lazy Loading
Hello,
I was watching this video by Kevin explaining a method on how to lazy load. (https://m.youtube.com/watch?v=mC93zsEsSrg&t=52s&pp=ygUPTGF6eSBsb2FkIGtldmlu)
In this video, he uses data-src attribute to hide / reveal images based on the Intersection Observer API in JS. However, after reading more about data attributes:
“Do not store content that should be visible and accessible in data attributes, because assistive technology may not access them. In addition, search crawlers may not index data attributes' values.”
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
Is it bad practice to use a lazy loading method with data tags? Considering crawlers may not recognize the data, would this be problematic in terms of SEO? Let’s say for example, you want to have a marketplace’s directory lazy load, but also index each market offer? Or, does it not matter, considering the page the offer redirects to will be indexed?
(Theoretical question)
Kevin Powell
YouTube
How to lazy load images
Continuing my dive into the Intersection Observer API, this week I look at probably the most practical use case for them, lazy loading images! It's relatively easy to do, and we can use the intersection observer's rootMargin to make sure the image loads before it enters the screen.
Other videos in this series:
Introduction to Intersection Obse...
Using data attributes - Learn web development | MDN
HTML is designed with extensibility in mind for data that should be associated with a particular element but need not have any defined meaning. data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, or extra properties on DOM.
27 Replies
now-a-days, a better method is to use the
loading="lazy"
attributeDoes this attribute automatically work depending on viewport?
yes
Interesting, very simplified solution compared to what I was looking at
keep in mind that this doesn't work in older browsers
Just out of curiosity, with that attribute, is it possible to lazy load in order?
For example, load Box #1, then Box #2, then Box #3?
https://caniuse.com/loading-lazy-attr <-- it has pretty good support
no
it loads in whatever order
i would assume it loads in dom order, but that's just a guess and i doubt it will ever be consistent
okay, so assuming this row took up the entire viewport, it would load all three elements at the same time
it would start loading them at roughly the same time, depending on the number of available connections the browser has, if you use http 1.0 and 1.1
the keyword is start
it will end loading whenever it ends
okay cool makes sense
How would you recommend achieving this solution?
don't
seriously, don't bother
Why not?
this is one reason
For ecommerce, it looks pretty clean when stuff gradually loads
animate it in css instead
or in javascript
Yeah. The video uses Observer API. I'm assuming a solution where you check whats in viewport, then loop through elements and apply css transition to each element with a delay could be a method at gradually loading
the images should be already loaded at that point
Just curious how to handle the actual images. The data-src attribute makes sense, just doesn't seem SEO friendly
it doesn't matter
the
alt
attribute is what matters for seoOkay
https://pagespeedchecklist.com/lazy-load-images <-- i did find this, which is an interesting take
Native Lazy Image Loading With JavaScript Fallback
Lazy load images to reduce initial-load page weight with the browser-native loading="lazy" attribute and a simple JavaScript fallback.
In the JS example for fallback, how does it detect if
loading="lazy"
is supported by browser or not?
That's !('loading' in HTMLImageElement.prototype))
?basically, yes
if the
'loading'
key doesn't exist in the HTMLImageElement
prototype, then the browser doesn't have support for itJust to make sure I'm understanding.
If a browser doesn't support an attribute, such as loading, it will not be defined in the markup if you were to inspect page code?
hence checking to see if loading is a property of the image
no, it won't be defined in whatever that feature is
it's very generic
but the idea is simple
if a browser has the
'loading'
key in the prototype, then that means the browser supports the loading
propertyOkay that makes sense
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement
Under browser compatibility, those are all of the properties that would be potentially returned if you
console.log(HTMLImageElement.prototype)
?
Sorry for the additional question. I haven't seen .prototype beforeif the browser supports it, it will be available