DEV Community

Cover image for How I got perfect  Google Lighthouse score with Gatsby
Jessica Valencia
Jessica Valencia

Posted on • Edited on

How I got perfect Google Lighthouse score with Gatsby

A few weeks ago, I got assigned a project to open a website for SEO. Targeted keyword is casino know-hows and online casino website, which are very competitive in local.

I have used WordPress and Wix for my SEO projects before. But this time, I decided to use Gatsby because:

  • It should be blazingly fast for more competitive power
  • The website will be purely static
  • I wanted to have source code under my control
  • I wanted to challenge a new technique
  • And of course, it's React, so why not?

It took me 8 days for me to build a website from scratch and deploy to a server. The website 카지노 has 46 pages.

카지노 | 바카라 | 먹튀 검증 - 카지노 하우스

Google lighthouse and gtmetrix score is fairly high.

카지노 사이트 추천 먹튀 검증 카지노 하우스 casinohouse.link google lighthouse score

바카라 사이트 온라인 카지노 사이트 모음 카지노 하우스 casinohouse.link gtmetrix score

* Lighthouse score may vary according to locations and browsers

I'd like to share my experience passing google lighthouse audits:

Serve images in next-gen formats

You may have seen this message almost every time optimizing your website performance.

Google lighthouse really likes webp but webp images are notorious for not working with iOS browsers. There are workarounds like using <picture> tag, but it's a repetitive and dull work. I'm sure many developers give up with webps and just stick to jpg images.

With gatsby-image, you won't have this headache ever again.

For example, I use graphQL queries like this one:

query BlogPostByID($id: String!) {
  markdownRemark(id: { eq: $id }) {
    html
    fields {
      slug
    }
    description
    tag
    featuredimage {
      publicURL
      childImageSharp {
        fluid(maxWidth: 2048, quality: 64) {
          ...GatsbyImageSharpFluid_withWebp
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Then in my component, I render like this:

import Img from 'gatsby-image'

<Img fluid={image.childImageSharp.fluid} />
Enter fullscreen mode Exit fullscreen mode

Then in my html, I get element like this one:

바카라 사이트 모음 카지노 사이트 추천 안전놀이터 카지노 하우스 casinohouse.link webp

Notice that there is an img tag with base64 encoded source. It is the blurred version of the original image. It's first shown before high-quality image is fully loaded. gatsby-image-sharp will crop and resize your images and generate thumbnails when you build your pages. All images are sized correctly and much network payload is saved.

It was like magic!

Gotcha

Please notice that I set webp quality as 64. It is known that lossless (or with 100% quality) webp compression will increase the size by 30%.

In other words, if you set quality to 100, webp image size will be bigger than your original image size.

Serve static assets with an efficient cache policy

Lighthouse requires you to return Cache-Control HTTP response header like this one:

Cache-Control: max-age=31536000
Enter fullscreen mode Exit fullscreen mode

You can find how to set Cache-Control header in nginx and apache in this artice.

Gotcha

31536000 actually means 365 days or a year. I tried to reduce this value to 10 days(or 864000) but the warning message did not disappear. So I think you should leave it as it is.

Enable text compression

Gzip compression greatly reduces network bytes by compressing text-based resources.

Here you can find how to enable gzip compression in apache and nginx.

Background and foreground colors have a sufficient contrast ratio

I've seen many websites fail this audit. You might have to change your color palette if you want to pass this one.

According to lighthouse:

  • Text that is 18pt, or 14pt and bold, needs a contrast ratio of 3:1,
  • All other text needs a contrast ratio of 4.5:1

You can put Chrome DevTools's color picker to good use to check your contrast ratio:

color picker

Gotcha
If the background uses an image or animated css, lighthouse may not decide contrast ratio. In this case, the audit will be failed although it has enough contrast to the human eyes.

For example, casinohouse.link footer uses css animation. I couldn't persuade lighthouse that the footer had enough contrast ratio. As a workaround, I added another background color to links. And it looked better in that way.

2020 한국 1위 카지노 커뮤니티 먹튀검증업체 메이저업체 카지노 하우스

Use HTTP/2 for all of its resources

My hosting web server uses CentOS and apache. Unfortunately, it uses HTTP1.1. I found out that it was not that easy to make it use http2. As a workaround, I enabled cloudflare for my domain. Then, the problem solved within a minute. And it was even FREE!

Conclusion

All the other audits were already handled by Gatsby. It was really convinient. Although this was the first time I used a Static Site Generator, I got satisfying result and I think my selection was perfect.

Please take a look around of my website and feel free to suggest any ideas and further optimizations.

Thanks for reading!

Top comments (17)

Collapse
 
quinncuatro profile image
Henry Quinn

It can be tricky - I've noticed some things drop below 100 for me on my Gatsby site.

Just went through another round of shoring things up last week to get to 100 across the board!

henryneeds.coffee

Collapse
 
jessica000 profile image
Jessica Valencia

Numbers are numbers. I like your site!

Collapse
 
quinncuatro profile image
Henry Quinn

Thanks! It's gone through a lot of small iterations over the years but it's always been that terminal theme.

I think I'm going to mess with the CSS a bit to make different sections of it more clear, but then I'll let it sit for a bit, haha.

Collapse
 
perpetual_education profile image
perpetual . education • Edited

We gotta learn more about HTTP2 !

It's important to remember that Lighthouse is not ONLY a tool to help the people but also to help Google sell you the idea of AMP pages (which are never going to take hold... but all the same). By telling you that your site isn't fast enough... they are really pushing that idea a bit far... with the goal of keeping you reliant on them. Netlify seems to be building a similar idea. How could we possibly create 100/100 HTML pages - with React? :/

Setting a reasonable performance budget is good enough. Just having a vimeo video on your site is going to knock you a little / and be out of your hands. Not all sites are markdown templates. All sites have different goals and different constraints. 100/100 is not the primary goal.

We really liked Scott Jehl's mini-course on web performance.

Collapse
 
jessica000 profile image
Jessica Valencia

Couldn't agree with you more. If I make a dynamic website, 80 ~ 90 points will be more than enough. But I wanted to make a fast static website. When you see score like 93, you want to make it 100, right?

Collapse
 
perpetual_education profile image
perpetual . education

Hey, we're super into being 100 ;)

for sure. Gotta go for the gold. But there's a lotta people who care more about that 100 than having any decent CSS / or content... and so - our comment is just a little shout-out to those people. : )

Collapse
 
ender_minyard profile image
ender minyard

Awesome!

Collapse
 
jessica000 profile image
Jessica Valencia

This is my first day on DEV community.
Thanks for cheering me!!

Collapse
 
ender_minyard profile image
ender minyard

💞 I like DEV, I hope you like it too :-)

Collapse
 
stevepepple profile image
Steve Pepple

This guide was very helpful, thanks much!
In particular, I'm using Wordpress + Gatsby I didn't understand why my wepb images were larger than the source in Wordpress.

Still have some things to optimize for our site, Vibemap.com, but these tips are a solid improvement.

Collapse
 
ben profile image
Ben Halpern

Really great post

Collapse
 
jessica000 profile image
Jessica Valencia

Thanks, Ben!

Collapse
 
bravemaster619 profile image
bravemaster619

Superb!

Collapse
 
dutradotdev profile image
lucas

Nice!

Collapse
 
yurieastwood profile image
Yuri Eastwood

Nice post, thanks for sharing! (:
We just have to remember to keep that balance of performance (or achieving tool's requirements, which is not always the same) and usability/focus on the end goals.

Collapse
 
jessica000 profile image
Jessica Valencia

Totally agree with you!

Collapse
 
blossom profile image
Blossom Babs

do you have any idea how to Serve static assets with an efficient cache policy with a site hosted on netlify

Some comments may only be visible to logged-in visitors. Sign in to view all comments.