Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to Improve Cumulative Layout Shift (CLS) with Gatsby

9 replies
Last updated: Nov 27, 2020
how do I achieve this same result and avoid this content jankiness (image pushing down content) in Sanity? https://css-tricks.com/do-this-to-improve-image-loading-on-your-website/
I am currently editing
Blog with Gatsby
template but so far dumbfounded on how to solve this with sanity (gatsby-image seems to do it for you). Do I modify in this file here? https://github.com/ajmalafif/afif.dev/blob/develop/web/src/lib/image-url.js
or somehow I can achieve this “lazy-loading” effect by combining both
gatsby-image
and
sanity/image-url
? https://github.com/sanity-io/gatsby-source-sanity#fluid
Nov 25, 2020, 5:37 PM
it seems from observation and this is anecdotal, seems like Gatsby solution do something like this to avoid the content jump https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/
will investigate how can I figure this out for sanity on gatstby
Nov 25, 2020, 5:45 PM
I just published gatsby-plugin-sanity-image to provide an easy way to handle this. Use the
ImageWithPreview
version to avoid the CLS issue.
Nov 25, 2020, 6:12 PM
shout out to
user L
now I have a solid base to start figuring this jank-free content layout. With
Slow 3G
network on devtools I have
lqip
right away!
Nov 26, 2020, 4:55 PM
kinda looks this with
gatsby-image
from the markup it seems like it does something differently
Nov 26, 2020, 4:57 PM
You can get those thumbnails to scale up and keep your content from jumping around by putting the image in a container sized how you want, then setting the CSS on the image to
display: block; width: 100%
. Unless you are cropping the image, the LQIP should be the same aspect ratio. If you are cropping the image, you can instead set the absolute dimensions on the img element and use
object-fit: cover
on it.
Nov 26, 2020, 5:00 PM
again
user L
to the rescue! thanks that’s a really good pointer
Nov 26, 2020, 5:20 PM
user L
is there a way to enable discussion tab on https://github.com/coreyward/gatsby-plugin-sanity-image/ since I encounter difficulties on resizing the
lqip
which I am not sure if it’s a bug (hence i dont think i should create an issue ticket) but I also want to ask how since I tried and failed
Nov 27, 2020, 6:14 AM
figured it out by setting the CSS for
[data-lqip]
and its all worked out now! highly recommend this plugin! Now I know how to make the site jank-free 🥳
Nov 27, 2020, 8:27 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?