fix(assets): treat SVG width/height of zero as valid dimensions#16674
Closed
OfirHaf wants to merge 2 commits into
Closed
fix(assets): treat SVG width/height of zero as valid dimensions#16674OfirHaf wants to merge 2 commits into
OfirHaf wants to merge 2 commits into
Conversation
When an SVG has explicit width="0" or height="0" attributes, the
image metadata extraction threw a NoImageMetadata error because both
check sites used JavaScript truthiness tests where 0 is falsy.
In vendor/image-size/types/svg.ts, the calculate() function fell
through the explicit-dimensions path when either dimension was 0 and
then hit the TypeError('Invalid SVG') throw.
In assets/utils/metadata.ts, the post-probe guard also rejected 0 via
the !result.height || !result.width condition.
Using 0 for width/height is a valid and recommended pattern for SVG
filter containers that should be hidden from screen readers:
<svg width="0" height="0" aria-hidden="true"></svg>
Fix: change both truthiness checks to null checks so that 0 is
accepted as an explicit dimension while null/undefined still signal
a missing value.
Fixes withastro#16633
🦋 Changeset detectedLatest commit: db36b7d The changes in this PR will be included in the next version bump. This PR includes changesets to release 435 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Merging this PR will not alter performance
Comparing |
fkatsuhiro
reviewed
May 10, 2026
Contributor
There was a problem hiding this comment.
@OfirHaf san
Oh, I'm so sorry! I missed your PR and implemented the same issue...
Since the Astro bot's suggestion seems to be correct even in integration tests, I think it would be a good idea to add a test, also serving as a way to prevent future recurrences.
I will share the test I created on my end here, so please use it as a reference.
Contributor
|
went with #16681 |
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 does this fix?
Closes #16633
SVG elements with explicit
width="0"orheight="0"attributes threwNoImageMetadata: Could not process image metadatainstead of being imported normally. This is a valid and common pattern for SVG filter containers hidden from screen readers:Root cause
Two falsy-zero checks along the metadata extraction path:
vendor/image-size/types/svg.ts— thecalculate()function:assets/utils/metadata.ts— post-probe validation:Changes
packages/astro/src/assets/utils/vendor/image-size/types/svg.ts: replace truthiness check with null check incalculate()packages/astro/src/assets/utils/metadata.ts: replace truthiness check with null check inimageMetadata()Both changes are minimal and surgical — they only affect the specific falsy-zero edge case while leaving all other behavior unchanged. SVGs with missing dimensions (null/undefined) still throw the appropriate error.