back to all posts

Image Optimization in Gatsby after bad Lighthouse audit

by Nabendu Biswas / June 13th, 2020

#javascript #react #gatsby
Series: Thewebdev-fixes

As most of you know my new blog site is live at

I recently did a lighthouse audit from google chrome console and my site failed in performance.

Lighthouse auditLighthouse audit

Now, one of the main reason is large images. All of the images(more then 2k) inside blogs comes from, two powerful and optimized cdn — and

The image which you see in homepage along with the blog title , comes from gatsby-image. So, they are highly optimized, but i still see some of them here. I will soon load small size files from them.

Culprit imagesCulprit images

But the real issue, as from above screenshot are the book image and the logo image. I am using normal tag in them, so it’s time to change them to gatsby-image.

To use the gatsby-image, we need to have the path in gatsby-config.js in gatsby-source-filesystem. But my problem was that, i already had the directory setup done for using gatsby-image in my posts.

You can find the details about how i set my production blog site, with gatsby-image in my upcoming book Gatsby Cookbook. You can find more details about it here.


I found good details about setting multiple gatsby-source-filesystem from this blog.

Next, we will use the Img inside our Advert.js, which is been called from both index.js and RightMenu.js file.

As usual we have to use graphql query to get the image and use it with useStaticQuery.


We also need to add a new style in advert.module.css file, because gatsby-image creates a div to wrap image and we need some top margin.


Now, our image looks perfect in desktop.


And also in mobile.

Mobile viewMobile view

Now, let’s remove the unwanted import from index.js file.


Also, remove the not passed props from Advert.


We will also remove both of them from RightMenu.js file.


Now, we will also change the <img /> in Navbar.js. We will use the same type of code as in Advert.js file. We will make use of useStaticQuery, to get the response back.


Next, we will show the image using the new Img tag.


And we will have the logo image been served by gatsby-image.

Logo imageLogo image

As you might have remembered from the start of the post, i had some more images in lighthouse audit which were of larger size.

Problem imagesProblem images

So, i did reduced their quality and hence size by an online reducer and replaced them.

Reduced imagesReduced images

I had pushed the code to github and so, my site got deployed in netlify. I ran the lighthouse audit again after the deployment and it had increased my performance. Also, there is no performance issue due to images.

Lighthouse auditLighthouse audit

This completes the image optimization for my site but there are still a lot of small fixes to be done.

You can find the code for the site in my github repo here.

Nabendu Biswas