Skip to content

experimental.svgOptimizer does not pass the file path to SVGO, so prefixIds uses the same prefix for every file #17728

Description

@myakura

Astro Info

Astro                    v7.2.2
Vite                     v8.2.1
Node                     v24.19.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

svgoOptimizer() calls SVGO without the path option:

// packages/astro/src/assets/svg/svgo.ts
optimize: (contents) => optimize(contents, config).data,

So SVGO plugins that derive a per-file value from the path cannot work. With prefixIds, every file falls back to SVGO's default prefix, and ids from different SVG files collide once they are inlined on the same page.

In the reproduction, one.svg and two.svg each define a gradient with id="a" — the id SVGO's own cleanupIds plugin emits. Both become prefix__a, so the second definition is unreachable and both squares render red:

<svg viewBox="0 0 24 24"><defs><linearGradient id="prefix__a"><stop offset="0" stop-color="red" /></linearGradient></defs><rect width="24" height="24" fill="url(#prefix__a)" /></svg>
<svg viewBox="0 0 24 24"><defs><linearGradient id="prefix__a"><stop offset="0" stop-color="blue" /></linearGradient></defs><rect width="24" height="24" fill="url(#prefix__a)" /></svg>

parseSvg() in packages/astro/src/assets/svg/utils.ts already has path in scope — it uses it for the CannotOptimizeSvg error message — but does not forward it to svgOptimizer.optimize().

What's the expected result?

Prefixes derived per file. Running SVGO directly with a path produces exactly that:

optimize(contents, { path: 'src/icons/one.svg', plugins: [{ name: 'prefixIds' }] }); // id="one_svg__a"
optimize(contents, { path: 'src/icons/two.svg', plugins: [{ name: 'prefixIds' }] }); // id="two_svg__a"

Link to Minimal Reproducible Example

https://github.com/myakura/test--astro-v7-svgo-prefixids

Built output: https://myakura.github.io/test--astro-v7-svgo-prefixids/

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)pkg: astroRelated to the core `astro` package (scope)triage: fix pendingReporter needs to verify the triage bot fix works

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions