Watch a live product demo 👀 See how Sanity powers richer commerce experiences

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

The Sanity Composable Content Cloud is the modern content platform that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing