
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! The best practice for responsive images from Sanity is to use the native HTML srcset and sizes attributes rather than calculating widths with JavaScript. Here's the recommended approach:
Instead of setting a single fixed width, generate multiple image URLs at different sizes and let the browser choose the most appropriate one:
function ResponsiveImage({image}) {
return (
<img
src={imageUrlFor(buildImageObj(image))
.width(800)
.url()}
srcSet={[
`${imageUrlFor(buildImageObj(image)).width(400).url()} 400w`,
`${imageUrlFor(buildImageObj(image)).width(800).url()} 800w`,
`${imageUrlFor(buildImageObj(image)).width(1200).url()} 1200w`,
].join(', ')}
sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1200px"
alt={image.alt || ''}
/>
)
}sizes AttributeThe sizes attribute tells the browser how wide the image will be displayed at different breakpoints. This helps it choose from your srcSet options. Adjust these values based on your actual layout:
(max-width: 600px) 400px - On small screens, image displays at 400px(max-width: 1200px) 800px - On medium screens, 800px1200px - Default for larger screensAs mentioned in the Sanity documentation on presenting images, reusing the same image transformations across your site ensures cached assets are used effectively, which is much better for performance than dynamically calculating unique widths.
Avoid using JavaScript to calculate viewport width and generate unique image URLs on every render - this defeats Sanity's CDN caching and can lead to usage overages.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store