March 23, 2021 • 2 min read
The images on our website load all at the same time, including the ones that are not in view, blocking other assets to be downloaded.
This makes our website take longer to load, and has a bad effect on our UX.
We are going to use a nice little package called LazySizes to make images load only when they are scrolled into view.
To do that we only need to:
src attribute on images with a data-src attribute, so the browser doesn’t download them automatically. Also, this allows LazySizes to take the image source in order to request it when scrolled into view.lazyload to the image, so LazySizes knows it has to act on that element.Here’s a quick demo, scroll to make images appear. You can check the network tab on the browser’s DevTools and you’ll see image requests being done as you scroll!