How to optimize image file sizes in Sanity Studio beyond width parameters?

11 replies
Last updated: Nov 26, 2021
Hi everyone, happy Friday! 🎉
I wonder if someone can help me when it comes to images.

Some of the images we are uploading to Studio are very high res which results in file sizes of between 1 - 3Mb which is being downloaded client side and it obviously not good.

I am already appending URL parameters to the asset url and specifying min & max widths for the images, however this doesn't seem to optimize the file size of the image.

Is it possible to return an optimized file size for an asset or do we need to optimize the assets before uploading them to Studio?

Thank you.
AI Update

Good news! You're on the right track with using URL parameters, but you need to add a couple of key parameters to properly optimize your images. The file sizes you're seeing are large because you're only specifying dimensions without compression optimization.

Add Quality and Format Parameters

The most important parameters for reducing file size are:

  1. auto=format - This automatically serves the most optimized format (WebP/AVIF) based on browser support
  2. q=75 - Controls compression quality (0-100, default is 75 for JPG/WebP)

So instead of just:

?w=800&h=600

Use:

?w=800&h=600&auto=format&q=75

Example with Lower Quality

If you need even smaller files, you can reduce the quality parameter:

?w=800&h=600&auto=format&q=60

Quality around 60-75 typically provides a good balance between file size and visual quality.

Using @sanity/image-url

If you're using the @sanity/image-url helper library, you can do this programmatically:

import imageUrlBuilder from '@sanity/image-url';

const builder = imageUrlBuilder(client);

const url = builder
  .image(imageAsset)
  .width(800)
  .height(600)
  .auto('format')
  .quality(75)
  .url();

What These Parameters Do

  • auto=format: Automatically returns WebP for supported browsers (much smaller than JPG) and can even serve AVIF for newer browsers (even smaller than WebP). This is determined by the browser's Accept header.
  • q (quality): Controls compression - lower values = smaller files but more compression artifacts. Defaults are 75 for JPG and WebP.

Additional Optimization Tips

  • Use fit=max if you don't want to scale images UP beyond their original size
  • Consider using responsive images with different sizes for different screen sizes
  • The CDN automatically caches transformed images, so subsequent requests will be fast

Important Note

You don't need to optimize images before uploading to Studio - Sanity's image pipeline handles all the optimization on-the-fly through these URL parameters. This is actually one of the key benefits of using Sanity's asset CDN! Upload high-resolution originals and let Sanity's CDN serve optimized versions based on your URL parameters.

The combination of auto=format and an appropriate quality setting should significantly reduce your file sizes from 1-3MB to much more reasonable sizes for web delivery.

Show original thread
11 replies
Hi User! Our image pipeline should handle that for you. Intention is that you should be able to store your originals. You can force jpg, specify quality params and use
format=auto
to get
webp
for browsers that can handle it.
Thank
user A
. I am still however get an asset back that is 1 - 3Mb though as that is what we upload to Studio.
Have a URL?
hm. in chrome i’m getting that as a 200Kb webp file, but it’s still a lot of pixels. looks like you’re missing a
&
in the above and when I just give it a width I’m getting a scaled
webp
back https://cdn.sanity.io/images/r50v8e4h/production/6a912497854e88e7daa8fded79ac53247eb3af7e-4800x2400.png?auto=format&w=100
Thank you,
user A
. There was an
&
missing.
But what if I want a max width of 2000 in the smallest file size?
🤔
Instead of using an explicit width as per your example?
Unless you’re using this with a hotspot/crop and fit modes the server side won’t have any context on what size you actually want. 🤔
Thank you,
user A
.
If you want to use hotspot / crop functionality and have it depend on client display I’d use a helper to generate the right URL https://www.sanity.io/docs/image-url
Alternatively you could craft srcset URLs using widths/heights.

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.

Was this answer helpful?