mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #236] [MERGED] fix: handle SVGs in image optimization to match Next.js behavior #409
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#409
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/236
Author: @southpolesteve
Created: 3/3/2026
Status: ✅ Merged
Merged: 3/3/2026
Merged by: @southpolesteve
Base:
main← Head:fix/svg-image-support📝 Commits (1)
fcbf801fix: handle SVGs in image optimization to match Next.js behavior (#205)📊 Changes
7 files changed (+297 additions, -25 deletions)
View changed files
📝
packages/vinext/src/config/next-config.ts(+6 -0)📝
packages/vinext/src/deploy.ts(+14 -1)📝
packages/vinext/src/index.ts(+43 -0)📝
packages/vinext/src/server/image-optimization.ts(+43 -12)📝
packages/vinext/src/server/prod-server.ts(+26 -9)📝
packages/vinext/src/shims/image.tsx(+16 -3)📝
tests/shims.test.ts(+149 -0)📄 Description
Summary
Fixes #205. SVGs passed to
/_vinext/imagenow work correctly, matching Next.js behavior.Based on the approach from #215 by @illegalcall (Dhruv Sharma). Thank you for the contribution! This PR takes the same direction with some fixes to match Next.js behavior more precisely.
What changed
Client-side: auto-skip SVGs (default behavior)
When
srcends with.svganddangerouslyAllowSVGis not enabled in config, theImagecomponent automatically setsunoptimized = true. This means SVGs bypass/_vinext/imageentirely and load directly from their source URL. This matches what Next.js does.Server-side: SVG passthrough with security headers
When
dangerouslyAllowSVG: trueis set innext.config, SVGs that reach the image optimizer are passed through without transformation (no sharp processing). Security headers are applied:Content-Security-Policy: script-src 'none'; frame-src 'none'; sandbox;Content-Dispositionset per config (default:inline)X-Content-Type-Options: nosniffNew config options in
next.config.imagesdangerouslyAllowSVG(default:false) - allow SVGs through the optimizercontentDispositionType(default:"inline") - controls Content-Disposition headercontentSecurityPolicy(default:"script-src 'none'; frame-src 'none'; sandbox;")How Next.js does it
From Next.js source (
shared/lib/get-img-props.ts):The auto-skip is conditional on
!dangerouslyAllowSVG, not unconditional. This is the key difference from #215, which always skipped SVGs regardless of config.Files changed
config/next-config.ts- Added SVG-related config typesshims/image.tsx- Client-side conditional SVG auto-skipserver/image-optimization.ts- SVG passthrough logic, configurable security headers,ImageConfigtypeindex.ts- InjectdangerouslyAllowSVGconfig into client and build outputsserver/prod-server.ts- Thread image config through to optimization handlerdeploy.ts- Pages Router worker entry passes image configtests/shims.test.ts- Unit tests for all SVG handling pathsTests
Added tests covering:
dangerouslyAllowSVGis false (default)dangerouslyAllowSVGis trueisSafeImageContentTypeaccepts/rejects SVG based on confighandleImageOptimizationdangerouslyAllowSVGis false🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.