Contentful logo

Contentful Community

Automatic image format selection

We’re using Contentful in combination with Next.js (and next/image), which allows us to intelligently transform images for different browsers.

Unfortunately, this doesn’t support switching image formats, preventing us from delivering next-gen images to modern browsers.

Does Contentful have any plans to add a auto option to the Image API’s format parameter? (determining the optimal format with the Accept header or User Agent sniffing)

Ideally, this would behave like Cloudinary’s f_auto parameter

Alternatively, are there any workarounds to enable this?

2 Likes

Agreed that this would be a great addition to the Image API. I’m in the same situation with Next and would like to use next/image but due to this restriction I can’t. My workaround is to use a custom component for the picture element and by specifying multiple source elements with webp and jpeg fallbacks you can mimic this behaviour. But this makes it a lot trickier to preload images using <link rel="preload"> so relying on loading="eager" is probably the best option until this is resolved.

Would be great if Contentful would consider adding this feature.

1 Like

For anyone stumbling across this, our approach has been to miss out on image format optimization, but take advantage of resolution optimization with the following loader:

function contentfulImageLoader({ src, width, quality }: ImageLoaderProps): string {
  return `${src}?w=${width}&q=${quality || 75}&fit=fill&f=face`;
}

An alternative would be to proxy all image through Next’s image optimizer, but it definitely doesn’t feel like an ideal solution.

Hello @asher,

Source sets might be the solution you need if you want to, for example, display WebP images for Chrome and JPEG fallback for Safari. I wrote an article about it to Contentful’s blog a while ago, hopefully it’s useful to you.

Using the <picture> element with multiple <source> is an option for serving the correct image format for the client but that disables the ability to preload correctly using <link rel="preload" .../>.

With content negotiation we would be able to improve performance by both using preload as well as serving the best format for the current browser.

This is something that impacts everyone trying to increase image performance. And especially developers using NextJS (with next/image) with Contentful. Until it is added, will impact LCP.