Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-steaks-repeat.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/astro/src/assets/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions packages/astro/test/core-image-svg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { APIRoute } from 'astro';
import image from '../assets/zero.svg';

export const GET: APIRoute = async () => Response.json({ image });
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import ZeroSvg from '../assets/zero.svg';
---
<html>
<body>
<div id="zero-svg">
<ZeroSvg />
</div>
</body>
</html>
Loading