React Progressive Graceful Image with Lazy Loading
Published: 2020-02-23 12:07:11

---
published: true
firstPublishedAt: 1578336447432
latestPublishedAt: 1578336707454
slug: react-progressive-graceful-image-with-lazy-loading
title: React Progressive Graceful Image with Lazy Loading

---

![](https://cdn-images-1.medium.com/max/1280/1*btEjjGu76-cHqBA0Re-aDw.gif)

TLDR;

- npm i [react-progressive-graceful-image](https://www.npmjs.com/package/react-progressive-graceful-image)

- Supports features like Custom Image Placeholder/Loader Component, srcset, lazy Loading, Graceful Loading, Progressive Image Loading.

- Use of Intersection Observer and navigator.onLine (Better performance and Optimization)

# Motivation

I was working on an e-commerce PWA and you know what is there on an e-commerce platform? Images, more images and well, a lot more images. The truth is, with the increase in internet speed, images are everywhere now, be it e-commerce/social media/blog/news/dating or any other site.

![Image is Everything, Everything is Image — (Credit: Rockstar movie)](https://cdn-images-1.medium.com/max/1200/1*5Cn6Pwl_U0qwSCQjb0sHGQ.gif)

So what’s the big deal in loading images? Just make an img tag and done!! I am sure you are thinking it’s not even that easy. The problem is loading of an image takes time. Particularly, when there is a huge number of high-quality images on a single page.

---

# **Solutions**

To give a better user experience, various things can be done:

- Image Placeholder — Keep a static image(png/svg) / a loader(gif) / a colored background div(usually grey) / a grey div with shimmer effect like the one below, until the real image is downloaded.

![Image Loading with Shimmer effect animated placeholder](https://cdn-images-1.medium.com/max/640/1*shE9Vwcm-AiHynUfVIBuFQ.gif)

- Progressive JPEG — Use Progressive JPEG instead of a normal Baseline JPEG image, although it’s not much in the control of client-side code. Here is an example.

![Baseline Image vs Progressive Image Loading](https://cdn-images-1.medium.com/max/1180/1*Sd6bDFN6e-9-mvfvX9oXhQ.gif)

- Lazy Loading — Don’t load an image until it is visible or close to the viewport. Below gif explains it.

![Lazy loading](https://cdn-images-1.medium.com/max/1520/1*7pj15y_Im6511iPaxSw3cA.gif)

- Graceful Loading — Display the placeholder until the image is fully loaded and retry image loading even if network error happens(mostly when a device switches network for some reason like power/network failure).

- Progressive Image Loading — Sites like medium, facebook, etc uses a tiny version of the real image with blur filter until the real image is downloaded and then uses some CSS transition to replace the tiny one. Here is an example.

![Progressive Image Loading](https://cdn-images-1.medium.com/max/1742/1*34x4V0cDOF2Wk3Mma-CrYA.gif)

- srcset attribute —used to let the browser automatically choose image based on device screen size and/or device pixel ratio(DPR).

---

Huh? That’s a lot of information and so many things can be done. Now to build an <Image/> component in ReactJS with one or two of the above features is easy, but how about building an <Image/> component with all of the above features?

_Drumroll, please!! Enter the savior, the superhero…_

### **npm i [react-progressive-graceful-image](https://www.npmjs.com/package/react-progressive-graceful-image)**

---

# Some FAQs

- Why do we need various combinations together, why not implement just one variation, for example - Progressive Image Loading?

— Let’s take an example. Home Page of an e-commerce has various sections, some with ads for which tiny images might be available. Since it’s a landing page that gets maximum eyeballs of visitors, hence better user experience is required. But “Search result page” or “Product description page” may serve images for which tiny images are not available(why you ask? One of the reasons, it’s costly). So we need let’s say, a grey div placeholder with shimmer effect. And as a developer, you don’t want to make multiple components for images. Similarly, there can be other use-cases. You get my point.

- Why do we use this npm package? Is there any other better package?

— I found some really good packages like [react-shimmer](https://www.npmjs.com/package/react-shimmer), [react-graceful-image](https://www.npmjs.com/package/react-graceful-image), and [react-progressive-image](https://www.npmjs.com/package/react-progressive-image) but honestly speaking, most of them lacked one or more important features. Like, [react-graceful-image](https://www.npmjs.com/package/react-graceful-image) lacked shimmer effect and [react-progressive-image](https://www.npmjs.com/package/react-progressive-image) lacked lazy-loading and graceful-loading. In fact, I started by forking [react-progressive-image](https://www.npmjs.com/package/react-progressive-image) and new features from [react-graceful-image](https://www.npmjs.com/package/react-graceful-image). Later improved upon them.

- What about performance, optimization, and browser support?

— Currently, the package size is [3.6kb](https://bundlephobia.com/result?p=react-progressive-graceful-image@0.6.9). I have used Intersection Observer for Lazy Loading (Better Performance) as compared to throttling scroll event listeners which run on the main thread. One big reason, Intersection Observer runs on a separate thread in the Browser. Read more about it [here](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and see it in action at [@researchgate/react-intersection-observer](https://www.npmjs.com/package/@researchgate/react-intersection-observer) which is the only dependency of this package.

Also, the retry strategy for graceful loading in [react-graceful-image](https://www.npmjs.com/package/react-graceful-image) was not quite optimized. It would also hang the screen sometimes when the device goes offline and a lot of image requests are sent simultaneously which would fail anyway. So I have used _navigator.onLine_ based strategy for optimization.

In case Intersection Observer is not supported by a browser, its polyfill is available [here](https://www.npmjs.com/package/intersection-observer).

---

# Usage

Great!! How do I use it? Any demo?

here you go…

> [**react-progressive-graceful-image-simple-example - CodeSandbox**](https://codesandbox.io/embed/react-progressive-graceful-image-simple-example-h8jh4?fontsize=14&hidenavigation=1&theme=dark)
>
> <small>CodeSandbox is an online editor tailored for web applications.</small>

You can find more examples and documentation on [npm](https://www.npmjs.com/package/react-progressive-graceful-image) or [github](https://github.com/sanishkr/react-progressive-graceful-image).

In case you find any issues or improvements, PRs and contributions are welcome on github.

---

**Extra Browny Stuff:** Always use padding-percentage solution to wrap the <Image/> component to prevent any reflow and improve browser performance. [Here is a small code snippet](https://gist.github.com/sanishkr/7f520f227989133a779eb49726bc77cb) to make the best use of this logic.

---

_Thank you for reading this article. Please share if you liked it._