Skip to content

Commit a0d2fe3

Browse files
astro-factory[bot]astrobot-houstonmatthewp
authored
Pass file path to SVGO so prefixIds generates unique per-file prefixes (#17816)
* fix(assets): pass file path to SvgOptimizer so SVGO prefixIds generates unique prefixes The `SvgOptimizer` interface and `svgoOptimizer()` did not forward the file path to SVGO's `optimize()`. Plugins like `prefixIds` that derive per-file values from the path fell back to a generic prefix, causing ID collisions when multiple SVGs were inlined on the same page. - Add `path` parameter to `SvgOptimizer.optimize` signature - Forward `path` into SVGO config in `svgoOptimizer()` - Pass `path` from `parseSvg()` to `svgOptimizer.optimize()` Fixes #17728 * chore: add changeset --------- Co-authored-by: astrobot-houston <[email protected]> Co-authored-by: Matthew Phillips <[email protected]>
1 parent ae26d18 commit a0d2fe3

8 files changed

Lines changed: 79 additions & 3 deletions

File tree

‎.changeset/two-roses-lie.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes the experimental `svgOptimizer` not generating unique per-file ID prefixes when using SVGO's `prefixIds` plugin

‎packages/astro/src/assets/svg/svgo.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { optimize, type Config } from 'svgo';
55
export function svgoOptimizer(config?: Config): SvgOptimizer {
66
return {
77
name: 'svgo',
8-
optimize: (contents) => optimize(contents, config).data,
8+
optimize: (contents, path) => optimize(contents, { ...config, path }).data,
99
};
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export interface SvgOptimizer {
22
name: string;
3-
optimize: (contents: string) => string | Promise<string>;
3+
optimize: (contents: string, path: string) => string | Promise<string>;
44
}

‎packages/astro/src/assets/svg/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function parseSvg({
1717
let processedContents = contents;
1818
if (svgOptimizer) {
1919
try {
20-
processedContents = await svgOptimizer.optimize(contents);
20+
processedContents = await svgOptimizer.optimize(contents, path);
2121
} catch (cause) {
2222
throw new AstroError(
2323
{

‎packages/astro/test/core-image-svg.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,49 @@ describe('astro:assets - SVG Components', () => {
237237
});
238238
});
239239
});
240+
241+
describe('SVGO prefixIds', () => {
242+
let prefixFixture: Fixture;
243+
let prefixDevServer: DevServer;
244+
245+
before(async () => {
246+
prefixFixture = await loadFixture({
247+
root: './fixtures/core-image-svg/',
248+
experimental: {
249+
svgOptimizer: svgoOptimizer({
250+
plugins: [{ name: 'cleanupIds' }, { name: 'prefixIds' }],
251+
}),
252+
},
253+
outDir: './dist/core-image-svg-prefix-ids/',
254+
});
255+
256+
prefixDevServer = await prefixFixture.startDevServer();
257+
});
258+
259+
after(async () => {
260+
await prefixDevServer.stop();
261+
});
262+
263+
it('generates unique id prefixes per file', async () => {
264+
const res = await prefixFixture.fetch('/prefix-ids');
265+
const html = await res.text();
266+
const $ = cheerio.load(html, { xml: true });
267+
268+
const $one = $('#one svg');
269+
const $two = $('#two svg');
270+
assert.equal($one.length, 1);
271+
assert.equal($two.length, 1);
272+
273+
const oneGradientId = $one.find('linearGradient').attr('id');
274+
const twoGradientId = $two.find('linearGradient').attr('id');
275+
276+
assert.ok(oneGradientId, 'first SVG gradient should have an id');
277+
assert.ok(twoGradientId, 'second SVG gradient should have an id');
278+
assert.notEqual(
279+
oneGradientId,
280+
twoGradientId,
281+
`gradient ids must differ across files, but both are "${oneGradientId}"`,
282+
);
283+
});
284+
});
240285
});
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
import One from '../assets/gradient-one.svg';
3+
import Two from '../assets/gradient-two.svg';
4+
---
5+
<html>
6+
<body>
7+
<div id="one"><One /></div>
8+
<div id="two"><Two /></div>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)