next.js: Webp images using next/image are not converted to another format on incompatible browsers
What version of Next.js are you using?
10.0.4
What version of Node.js are you using?
12.16.0
What browser are you using?
Safari <= 13
What operating system are you using?
MacOS, iOS
How are you deploying your application?
next start, externally hosted assets
Describe the Bug
If you pass an image src which is natively a webp to the next/image component, it is always served as a webp. Safari versions <= 13 do not include webp support so the image fails to load.
The Accept header sent in the request to the _next/image endpoint from Safari looks like this:
Accept: image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5
I believe the following code is at fault: https://github.com/vercel/next.js/blob/356bcdec0302c13af312ddcc5c39e78ef8e40c9d/packages/next/next-server/server/image-optimizer.ts#L234-L240
Expected Behavior
On browsers that do not support webp, files should be converted into jpeg or png (most likely jpeg?).
To Reproduce
- Place a
.webpin yourpublicfolder or any other configured public host. - Use the
next/imagecomponent and set thesrcprop to point to the.webpimage. - Observe in Safari or IE or another non-webp supporting browser as the image is served in webp format and fails to render.
Perhaps it would also make sense to add an optional format(s) query param which the server accepts, passed through as a prop?
Also worth being aware of/considering is the HTML <Picture /> tag (MDN Docs)
Let me know if you’d like anything else from me, and I’ll be happy to lend a hand 😃
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 21
- Comments: 23 (2 by maintainers)
Why not use html to do the fallback. Newer browsers that support webp will load the source image url, older browsers fall back to the img tag with the jpg image.
<picture> <source srcSet="_next/image.webp?format=webp" type="image/webp" /> <img src="_next/image.webp?format=jpg" /> </picture>For me, this is not working. I am setting the images in JPEG format, hoping that it will convert it to webp for the browsers that support it, and it is not doing it.
This is the way I am using it
And I added in the next.config.js the domains that I am using
Maybe work well in Next v12.
I try to change the local file in
node_modules, it is work well.Another problems: If the web image contains transparent areas, it will be problematic to convert to JPEG.