Skip to content

Commit fd0d0f5

Browse files
authored
Exclude srcset from svg image (#44308)
The default behavior for svg is `dangerouslyAllowSVG: false` which means we won't try to optimize the image because its vector (see #34431 for more). However, svg was incorrectly getting the `srcset` attribute assigned which would contain duplicate information like: ``` /test.svg 1x, /test.svg 2x ``` So this PR makes sure we treat svg the same as `unoptimized: true`, meaning there is no `srcset` generated. Note that this PR won't change the behavior if `loader` is defined or if `dangerouslyAllowSVG: true`.
1 parent 8a9133d commit fd0d0f5

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

packages/next/client/image.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,11 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
528528
let loader: ImageLoaderWithConfig = rest.loader || defaultLoader
529529
// Remove property so it's not spread on <img> element
530530
delete rest.loader
531+
// This special value indicates that the user
532+
// didn't define a "loader" prop or "loader" config.
533+
const isDefaultLoader = '__next_img_default' in loader
531534

532-
if ('__next_img_default' in loader) {
533-
// This special value indicates that the user
534-
// didn't define a "loader" prop or config.
535+
if (isDefaultLoader) {
535536
if (config.loader === 'custom') {
536537
throw new Error(
537538
`Image with src "${src}" is missing "loader" prop.` +
@@ -625,6 +626,15 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
625626
if (config.unoptimized) {
626627
unoptimized = true
627628
}
629+
if (
630+
isDefaultLoader &&
631+
src.endsWith('.svg') &&
632+
!config.dangerouslyAllowSVG
633+
) {
634+
// Special case to make svg serve as-is to avoid proxying
635+
// through the built-in Image Optimization API.
636+
unoptimized = true
637+
}
628638

629639
const [blurComplete, setBlurComplete] = useState(false)
630640
const [showAltText, setShowAltText] = useState(false)

packages/next/shared/lib/image-loader.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ function defaultLoader({
5353
}
5454
}
5555

56-
if (src.endsWith('.svg') && !config.dangerouslyAllowSVG) {
57-
// Special case to make svg serve as-is to avoid proxying
58-
// through the built-in Image Optimization API.
59-
return src
60-
}
61-
6256
return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${
6357
quality || 75
6458
}`

test/integration/next-image-new/default/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ function runTests(mode) {
951951
).toBe('/test.svg')
952952
expect(
953953
await browser.elementById('without-loader').getAttribute('srcset')
954-
).toBe('/test.svg 1x, /test.svg 2x')
954+
).toBeFalsy()
955955
})
956956

957957
it('should warn at most once even after state change', async () => {

0 commit comments

Comments
 (0)