Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2020 09:09 pm GMT

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 casinohouse.link 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        }      }    }  }}

Then in my component, I render like this:

import Img from 'gatsby-image'<Img fluid={image.childImageSharp.fluid} />

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

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!


Original Link: https://dev.to/jessica50969989/how-i-got-perfect-google-lighthouse-score-with-gatsby-5coj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To