Lighthouse detecting invisible layout shifts ? (incredibly high CLS)

2.1k views Asked by At

I'm optimizing the speed of a WordPress website (on mobile only for the moment), with success except for CLS. The CLS became extremely high after optimizing CSS delivery (with WP-Rocket), but I don't see any layout shifts, even when I use Dev Tools (performance test). (on mobile)

Here’s the example : https://trustmyscience.com/israel-a-pratiquement-eradique-la-covid-19/

  1. Results here, with CSS delivery optimization

So it seems to be an invisible layout shift, that Lighthouse perceive as a real layout shift. Lighthouse shows me the problem comes from <div class="entry-content body-color clearfix link-color-wrap progresson">. So, it seems to be related to some "wrapping" of the all article content, that maybe shifts into the background (without being visible), because of some CSS rules maybe ?

  1. Here, the element with the highest layout shift
  2. The element with the highest layout shift (detail)

When I deactivate CSS delivery optimization, CLS go back to almost 0 (but LCP is too high).

  1. Results WITHOUT CSS delivery optimization

I need this CSS delivery optimisation because of LCP importance, but I also now need to solve this issue because of CLS introduction in Core Web Vitals, and need to find what Lighthouse is detecting as a LS. Also, maybe, Lighthouse needs a correction for that ? As it isn't a visible layout shift...

Do you have any idea on how to solve this ? Or do you think I need to reach LS developers to show them this ?

Thank you in advance for your help.

Regards,

2

There are 2 answers

2
jackdbd On

You might gain a bit in performance by self-hosting the fonts instead of making a call to the Google Fonts API, but fonts are not the main issue here. Javascript is.

There is a lot of Javascript on this website, so the main thread is busy downloading it, parsing it and executing it.

I ran both Lighthouse (with Clear Storage and Simulated throttling enabled) and WebPageTest with a Moto 4G profile.

As you can see from Chrome DevTools and WebPageTest, roughly 56% of the processing time spent on the main thread is due to scripting. Do you really need all of that Javascript?

Here is what I noticed in the Chrome DevTools Performance panel:

Chrome DevTools Performance panel showing JS requests

WebPageTest main thread processing breakdown

CLS is basically the sum of all unexpected layout shifts that occur on a page. As you can see from the dashed orange line in the chart below, the 4 .woff2 font files contribute to the CLS: the first layout shift occurs as soon as the fonts are fetched.

first layout shift caused by fonts

But as I said, I would focus on removing all unnecessary Javascript. In particular, I would examine third-party JS like the one coming from choices.consentframework.com, which takes 1730ms to load and represents 25% of the content size (see below).

choices.consentframework.com takes 1730ms to load

choices.consentframework.com is 25% of the content size

After JavaScript, focus on the images.

The Performance panel in Chrome DevTools shows a lot of requests for images. Are you fetching only the images that are in the viewport, or all the images that are on the page?

a lot of requests for images

Most of these images are WebP and seem already optimized, but there are a few GIFs, which are really bad for performance. It seems that these GIFs are served by https://www.viously.com/ (I guess it's an Ad Server, it's the first time I see it).

GIFs are bad for web performance

Last but not least, double-check that all of your <img> and <video> have size and width attributes set. Images and videos are replaced elements with intrinsic dimensions, and forgetting to set sizes for images in your HTML is a common cause of layout shifts.

See also this article by Addy Osmani for a few more tips on how to optimize CLS.

layout shifts

3
Fahim On

The CLS is visible, it's the font.

Don't you notice that when you visit the page, the text gets resized? That's a common cause of CLS.

How to solve?

Serve your fonts locally. Do not use any plug-in like OMGF. Do it manually.

  1. Download the fonts. Choose 2 fonts, one for body, another for headings. You won't need bold, italic, or bold+italic fonts. These will be applied by user's browser.
  2. Convert to woff2 (only woff2 is enough, didn't face any issues)
  3. Upload it to your server
  4. Add font face CSS to declare the fonts
  5. Apply the fonts using CSS elements
  6. Disable Google fonts if you're using any WP theme or builder
  7. Preload the fonts

This will solve the CLS problem, will also reduce the Total Blocking Time.