Slow TTFB on Sanity CDN image transformations with new query params
@sanity/image-urlto generate query param transformations. One observation is that when one or more query param changes value, the request takes quite some time where TTFB is pretty much all of the wait time.• Is anyone else experiencing this?
• Does it just take that long to generate a new version of an image server side maybe?
• Is there something that can be optimized in our transformations query params?
Example image url we have:
https://cdn.sanity.io/images/ <project-id>/<dataset>/<some-img>.png?rect=750,0,1406,1438&w=1112&h=1137&auto=format
After the initial long running request, requests for the same image with the same params are quick.
You're experiencing exactly what the Sanity image CDN is designed to do! Here's what's happening and how to optimize:
Yes, this is normal behavior
When you change query parameters (like rect, w, h, or auto=format), you're requesting a new image transformation that hasn't been generated yet. The first request has high TTFB because Sanity's Image Pipeline is:
- Processing the image transformation on-demand
- Generating the new variant (cropping, resizing, format conversion, etc.)
- Caching it in the global CDN
After that initial request, the transformed image is cached at edge locations worldwide, so subsequent requests are lightning-fast.
The key optimization: Use predefined image sizes
According to Sanity's documentation on the Image Pipeline, the most important optimization is:
Limit the number of unique size/crop combinations you request. Instead of generating unique images for every viewport width, use a predefined srcset with a fixed set of image sizes.
Here's an example of a well-optimized approach:
<img
srcset="
https://cdn.sanity.io/images/.../image.png?w=320&h=320&auto=format 320w,
https://cdn.sanity.io/images/.../image.png?w=640&h=640&auto=format 640w,
https://cdn.sanity.io/images/.../image.png?w=960&h=960&auto=format 960w,
https://cdn.sanity.io/images/.../image.png?w=1280&h=1280&auto=format 1280w
"
sizes="(max-width: 768px) 100vw, 50vw"
/>Additional optimization tips
Pre-warm critical images: If you know certain image transformations will be heavily used, you can request them during your build process to pre-populate the CDN cache
Use integer values: Non-integer values for
wandhparameters can cause performance issues or timeoutsLeverage
auto=format: This is great for automatically serving WebP/AVIF to supporting browsers, but note that AVIF transformations are generated asynchronously, so the first request might get WebP insteadConsider
fitparameter carefully: Usingfit=maxprevents small images from being scaled up unnecessarily
Your current URL looks fine - the rect, w, h, and auto=format parameters are all standard. The "slowness" on first request is just the cost of on-demand image processing, which is actually a feature that saves you from pre-generating thousands of variants!
Show original thread4 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
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.