diff --git a/.changeset/wet-steaks-repeat.md b/.changeset/wet-steaks-repeat.md new file mode 100644 index 000000000000..ff689cd066bf --- /dev/null +++ b/.changeset/wet-steaks-repeat.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where SVG images with `width="0"` or `height="0"` incorrectly threw a `NoImageMetadata` error instead of being treated as valid dimensions. diff --git a/packages/astro/src/assets/utils/metadata.ts b/packages/astro/src/assets/utils/metadata.ts index 5ac51fe2956f..6ef850234323 100644 --- a/packages/astro/src/assets/utils/metadata.ts +++ b/packages/astro/src/assets/utils/metadata.ts @@ -23,7 +23,7 @@ export async function imageMetadata( message: AstroErrorData.NoImageMetadata.message(src), }); } - if (!result.height || !result.width || !result.type) { + if (result.height == null || result.width == null || !result.type) { throw new AstroError({ ...AstroErrorData.NoImageMetadata, message: AstroErrorData.NoImageMetadata.message(src), diff --git a/packages/astro/src/assets/utils/vendor/image-size/types/svg.ts b/packages/astro/src/assets/utils/vendor/image-size/types/svg.ts index 3f43a7b13a96..7965eb441c6c 100644 --- a/packages/astro/src/assets/utils/vendor/image-size/types/svg.ts +++ b/packages/astro/src/assets/utils/vendor/image-size/types/svg.ts @@ -95,7 +95,7 @@ export const SVG: IImage = { const root = extractorRegExps.root.exec(toUTF8String(input)) if (root) { const attrs = parseAttributes(root[0]) - if (attrs.width && attrs.height) { + if (attrs.width != null && attrs.height != null) { return calculateByDimensions(attrs) } if (attrs.viewbox) { diff --git a/packages/astro/test/core-image-svg.test.ts b/packages/astro/test/core-image-svg.test.ts index 8b15ddb1922f..d72d4ca57cb1 100644 --- a/packages/astro/test/core-image-svg.test.ts +++ b/packages/astro/test/core-image-svg.test.ts @@ -150,6 +150,37 @@ describe('astro:assets - SVG Components', () => { assert.ok(json.image.src.startsWith('/')); }); }); + + // test for the components has no dimensions and the metadata is correct + describe('Metadata with zero dimensions svg image', () => { + it('successfully processes SVG with width="0" and height="0"', async () => { + const res = await fixture.fetch('/zero-dimensions'); + assert.equal(res.status, 200); + + const html = await res.text(); + const $ = cheerio.load(html, { xml: true }); + + const $svg = $('#zero-svg svg'); + // svg should be rendered with the width and height of 0 as specified in the source file, not overridden by default dimensions + assert.equal($svg.length, 1); + assert.equal($svg.attr('width'), '0'); + assert.equal($svg.attr('height'), '0'); + }); + + it('successfully returns metadata for an SVG image with zero dimensions', async () => { + const res = await fixture.fetch('/metadata-zero-dimensions.json'); + const json = await res.json(); + + assert.equal(json.image.width, 0); + assert.equal(json.image.height, 0); + assert.equal(json.image.format, 'svg'); + }); + + it('should not log NoImageMetadata errors for zero dimension svgs', () => { + const hasMetadataErrors = logs.some(log => log.message.includes('NoImageMetadata')); + assert.equal(hasMetadataErrors, false); + }) + }); }); describe('SVGO optimization', () => { diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/zero.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/zero.svg new file mode 100644 index 000000000000..32a4e229e819 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/zero.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/metadata-zero-dimensions.json.ts b/packages/astro/test/fixtures/core-image-svg/src/pages/metadata-zero-dimensions.json.ts new file mode 100644 index 000000000000..988859e45051 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/metadata-zero-dimensions.json.ts @@ -0,0 +1,4 @@ +import type { APIRoute } from 'astro'; +import image from '../assets/zero.svg'; + +export const GET: APIRoute = async () => Response.json({ image }); diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/zero-dimensions.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/zero-dimensions.astro new file mode 100644 index 000000000000..21dc3c7167d2 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/zero-dimensions.astro @@ -0,0 +1,10 @@ +--- +import ZeroSvg from '../assets/zero.svg'; +--- + + +
+ +
+ +