fix(next): measure SVGs with an XML/DOCTYPE preamble before <svg> - #98934
Open
bookasloth wants to merge 1 commit into
Open
bookasloth wants to merge 1 commit into
bookasloth wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What / Why
Fixes #71810.
After upgrading to v15,
next dev/next buildfails for a validapp/icon.svgwith:Root cause: metadata/image size detection goes through
getImageSize, which calls the bundledimage-size(1.2.1).image-sizeonly detects an SVG when its<svgroot 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<svgpast the detection window —image-sizethen throwsunsupported file type, which surfaces asInvalidImageFormatError/ "not a valid image file". These are valid, measurable SVGs (they still carry aviewBox), and v14 handled them.Verified against the vendored
image-size@1.2.1: the reportedicon.svg(with<svgat byte 722) throws, but the same buffer sliced from<svgreturns{ width: 400, height: 400 }.Fix
In
getImageSize, if the initial detection fails and the buffer contains an<svgroot 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)