Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude srcset from svg image #44308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/next/client/image.tsx
Expand Up @@ -528,10 +528,11 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
let loader: ImageLoaderWithConfig = rest.loader || defaultLoader
// Remove property so it's not spread on <img> element
delete rest.loader
// This special value indicates that the user
// didn't define a "loader" prop or "loader" config.
const isDefaultLoader = '__next_img_default' in loader

if ('__next_img_default' in loader) {
// This special value indicates that the user
// didn't define a "loader" prop or config.
if (isDefaultLoader) {
if (config.loader === 'custom') {
throw new Error(
`Image with src "${src}" is missing "loader" prop.` +
Expand Down Expand Up @@ -625,6 +626,15 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
if (config.unoptimized) {
unoptimized = true
}
if (
isDefaultLoader &&
src.endsWith('.svg') &&
!config.dangerouslyAllowSVG
) {
// Special case to make svg serve as-is to avoid proxying
// through the built-in Image Optimization API.
unoptimized = true
}

const [blurComplete, setBlurComplete] = useState(false)
const [showAltText, setShowAltText] = useState(false)
Expand Down
6 changes: 0 additions & 6 deletions packages/next/shared/lib/image-loader.ts
Expand Up @@ -53,12 +53,6 @@ function defaultLoader({
}
}

if (src.endsWith('.svg') && !config.dangerouslyAllowSVG) {
// Special case to make svg serve as-is to avoid proxying
// through the built-in Image Optimization API.
return src
}

return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${
quality || 75
}`
Expand Down
Expand Up @@ -951,7 +951,7 @@ function runTests(mode) {
).toBe('/test.svg')
expect(
await browser.elementById('without-loader').getAttribute('srcset')
).toBe('/test.svg 1x, /test.svg 2x')
).toBeFalsy()
})

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