Loading SVG with next/image and dangerouslyAllowSVG #53041
-
SummarySVG image urls with additional parameter not loading. DescriptionOur team recently faced the task of upgrading NodeJS from version 16 to 18 due to the upcoming end of support for Node 14 and 16 by Vercel. During the upgrade, we also needed to update NextJS from version 11 to version 12, along with several other packages. (Note: Everything was running fine before upgrade.) While performing the upgrade, we encountered a peculiar issue related to NextJS image support for SVG images. The problem arose with two different images that were sourced from the same place, a headless CMS. Surprisingly, one of the images displayed without any issues, while the other had trouble loading. The source URLs of both images looked something like this:
One crucial difference between the two URLs was the presence of the The parameter was appended by prismic (CMS) which uses imgixAPI for image optimization. I couldn't look further into the Next ImageAPI (specifically how it works when parameter are also used) except for this documentation. Additionally, the issue was fixed by setting
Thanks in advance. Additional informationnext config for image images: {
domains: [
"abc-demo.cdn.prismic.io",
"images.prismic.io",
],
}, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
When the next.js/packages/next/src/shared/lib/get-img-props.ts Lines 389 to 393 in 2072f86 If you know the content type is svg but the I highly recommend you avoid I also created a PR to update the docs: #60735 |
Beta Was this translation helpful? Give feedback.
-
just add Next.js is blocking SVG images by default when using the component unless you explicitly allow them using the dangerouslyAllowSVG option. |
Beta Was this translation helpful? Give feedback.
-
Should we follow the docs recommendations even if we're using imported (self-hosted) svg files to be fed to Image component? Or just svgs pointing to external urls? The svgs are looking with jagged edges without those props currently. 😕 |
Beta Was this translation helpful? Give feedback.
When the
src
prop ends with.svg
, then thenext/image
component will automatically setunoptimized
.next.js/packages/next/src/shared/lib/get-img-props.ts
Lines 389 to 393 in 2072f86
If you know the content type is svg but the
src
prop doesnt end with.svg
(your first example), then you can add theunoptimized
prop to get the same behavior.I highly recommend you avoid
dangerouslyAllowSVG
config unless you understand the security risks.I also created…