DEV Community

Cover image for How to Lazy Load Images

How to Lazy Load Images

Prasanna Shandilya on February 22, 2020

What is lazy loading images? Lazy loading is a technique that defers the loading of non-critical resources at page load time. Instead, these res...
Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan • Edited

I added lazy loading to my Jekyll blog a couple weeks ago, and the results were immediately noticeable. Now most of my pages get a solid 100 on PageSpeed Insights.

I wrote a Python script, thumb, that lets me quickly generate scaled-down thumbnails of each image in my assets folder. These are about 1 KB each and serve as fuzzy/blurry placeholders. I then use the IntersectionObserver API to replace the blurry images with the actual image.

On top of all that, I'm also using WebP with fallbacks and generating WebP copies for all of my images in one go with another Python script, webp, that basically just wraps around the cwebp and gif2webp CLI tools that Google ships.

Collapse
 
shandilyaprasanna profile image
Prasanna Shandilya

Thanks for sharing your experience.

Collapse
 
andersclark profile image
Anders Clark

Great intro-post for us that's never implemented lazy loading.
For the future it would be best of you did something similar for loading thumbnails/compressed/blurred images before real ones for performance.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

How is this library? github.com/aFarkas/lazysizes

Collapse
 
shandilyaprasanna profile image
Prasanna Shandilya

lazysizes is a good well-documented library, I have personally never used it.
there are other library options as well:-
->blazy
->lozad.js
->react-lazyload

The motive of this article was to show how lazy load works under the hood. Otherwise, you can always grab a library and call it a day.