Skip to content

fix(next): measure SVGs with an XML/DOCTYPE preamble before <svg> - #98934

Open
bookasloth wants to merge 1 commit into
vercel:canaryfrom
bookasloth:fix/71810-svg-icon-preamble
Open

bookasloth wants to merge 1 commit into
vercel:canaryfrom
bookasloth:fix/71810-svg-icon-preamble

Conversation

@bookasloth

Copy link
Copy Markdown

What / Why

Fixes #71810.

After upgrading to v15, next dev / next build fails for a valid app/icon.svg with:

Error: Image import "...icon.svg" is not a valid image file. The image may be corrupted or an unsupported format.

Root cause: metadata/image size detection goes through getImageSize, which calls the bundled image-size (1.2.1). image-size only detects an SVG when its <svg root appears near the start of the buffer. SVGs exported by tools like Adobe Illustrator begin with an XML declaration, comments, and a <!DOCTYPE ... [ <!ENTITY ...> ]> block, which pushes <svg past the detection window — image-size then throws unsupported file type, which surfaces as InvalidImageFormatError / "not a valid image file". These are valid, measurable SVGs (they still carry a viewBox), and v14 handled them.

Verified against the vendored image-size@1.2.1: the reported icon.svg (with <svg at byte 722) throws, but the same buffer sliced from <svg returns { width: 400, height: 400 }.

Fix

In getImageSize, if the initial detection fails and the buffer contains an <svg root past the start, retry from that root before giving up. Non-SVG / genuinely-corrupt buffers still throw as before.

Added a unit test covering an SVG with a large preamble and a non-image buffer.


Contributed by Shubham Datarkar (thekalamwala)

The bundled `image-size` only detects an SVG when its `<svg` root appears near
the start of the buffer. SVGs exported by tools like Adobe Illustrator begin
with an XML declaration, comments and a DOCTYPE with entity declarations, which
pushes `<svg` past the detection window and makes `image-size` throw
"unsupported file type". This surfaced as `next dev`/`next build` failing with
"Image import ... is not a valid image file" for a valid `app/icon.svg` after
upgrading to v15.

Retry size detection from the `<svg` root when the initial call fails, so
these valid SVGs are measured instead of rejected.

Fixes vercel#71810
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get an error when upgrading to next v15 and icon.svg in the app folder

1 participant