How to Generate an Image URL Using Auto=Format

6 replies
Last updated: May 6, 2022
Hi, quick question:when generating a url for my images in the front-end there’s the option to use
auto=format
to automatically use
webp
as image format if the browser supports it. is there a way to specify a fallback format (eg i want to use
jpg
if the original image file is uploaded as
png
) if the browser doesn’t?
AI Update

Based on Sanity's image transformation documentation, there isn't a built-in fallback format parameter when using auto=format.

Here's how auto=format works:

When you use ?auto=format, Sanity automatically serves the most optimized format based on the browser's Accept header:

  • WebP for browsers that support it
  • AVIF for browsers that support it (though this is generated asynchronously, so the first few requests might get WebP instead)
  • Falls back to PNG/JPG depending on the original source image format if the browser doesn't support modern formats

The key point: The fallback is determined by your original uploaded image format, not by a parameter you can specify. If you upload a PNG, browsers that don't support WebP/AVIF will get PNG. If you upload a JPG, they'll get JPG.

Your options:

  1. Control the fallback by choosing your upload format: Upload your images as JPG if you want JPG as the fallback format for browsers that don't support WebP/AVIF.

  2. Use the fm parameter explicitly: Instead of auto=format, you can specify the format directly with fm=jpg or fm=webp, but this means you lose the automatic format selection benefit.

  3. Handle it client-side: Use picture elements with multiple source tags to specify your own fallback chain:

    <picture>
      <source srcset="image.jpg?fm=webp" type="image/webp">
      <source srcset="image.jpg?fm=jpg" type="image/jpeg">
      <img src="image.jpg?fm=jpg" alt="...">
    </picture>

Unfortunately, there's no single parameter like ?auto=format&fallback=jpg in Sanity's image pipeline. The fallback format is inherently tied to your source image format when using auto=format.

Show original thread
6 replies
Nah. If you want to use JPG because your image doesn’t need transparency, upload it as JPG in the first place instead of PNG. 🙂 The tool cannot know whether you need the transparency or not, and there is no argument to enforce a format as far as I know.
you can force formats with the urlBuilder
$urlFor(image.asset).format('jpg')
and a lot of clients just want to be able to upload images no matter the format. transparency doesn’t matter in most cases. (but i would generally agree
🙂)
Oh that's neat, I forgot about the URL builder. I wasn't aware, apologies. Thanks Bastian! ❤️
Not that I am aware of
user U
, no. Outside of the
auto
or
fm
URL parameters there isn't anything like what you are asking for to my knowledge.
You may be able to handle this depending on use case through client side logical conditionals, but this is not ideal from what you are seemingly looking for 🙇‍♂️
it probably doesn’t even matter anyway. the 2.5 people still using a browser that doesnt support webgl probably have different problems than downloading 200kb more because they are being served a PNG instead of a JPG… 😄 thanks

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?