How to improve image loading with Gatsby and gatsby-plugin-sanity-image?

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.

Categorized in

Related topics

Get more help in the community Slack

Go to How to deploy Gatsby sites using official Sanity starter templatesGo to How to generate RSS.xml files from Sanity posts with gatsby-plugin-feedGo to How to make Algolia work with Gatsby & SanityGo to Set up Draft Previews with Gatsby Cloud HostingGo to How to display Sanity images in Gatsby-powered photography sitesGo to Is it possible to use gatsby-image to render images loaded in Portable Text / Rich Text?
TopicCategoriesFeaturedRepliesLast Updated
How to deploy Gatsby sites using official Sanity starter templates25Nov 30, 2020
How to generate RSS.xml files from Sanity posts with gatsby-plugin-feed6Jan 12, 2021
How to make Algolia work with Gatsby & Sanity8Jan 7, 2021
Set up Draft Previews with Gatsby Cloud Hosting4Aug 26, 2020
How to display Sanity images in Gatsby-powered photography sites19Jun 16, 2020
Is it possible to use gatsby-image to render images loaded in Portable Text / Rich Text?8Jan 1, 2021

Related contributions

Occo
- Made with Sanity

Complex product offering with nested product structure and tiered pricing.

Kevin Green
Go to Occo

Figma Config
- Made with Sanity

Website for Config, the global design conference by Figma.

Corey Ward
Go to Figma Config

The Agnes
- Made with Sanity

A high end rental real estate site built with Gatsby, Sanity and Shopify that pulls in listing data via API and includes a headless Shopify shop.

David Gross
Go to The Agnes