“Cannot optimize SVG.” appears when you’ve enabled Astro’s experimental SVG optimizer and it fails on an imported SVG. The message names the file and optimizer: “An error occurred while optimizing SVG file “src/assets/logo.svg” with the “svgo” optimizer.” The hint is “Review the included error message provided for guidance.” That included cause, from SVGO, tells you whether the SVG markup is malformed or your optimizer config is wrong.
Key Takeaways
- The error only happens with
experimental.svgOptimizerset inastro.config.mjs.- The cause below the message is SVGO’s own error; read it first.
- Most failures are malformed SVG exported from design tools: unescaped
&, unclosed tags, or stray text before<svg>.- Re-export the file, or validate it in a browser, then rebuild.
Where the error comes from
Since Astro 6.2, you can optimize every imported SVG at build time:
// astro.config.mjs
import { defineConfig, svgoOptimizer } from 'astro/config';
export default defineConfig({
experimental: {
svgOptimizer: svgoOptimizer(),
},
});
When you import an SVG as a component (import Logo from '../assets/logo.svg'), Astro passes its contents through the optimizer. If SVGO throws, Astro wraps that in “Cannot optimize SVG.” with the file path. Without the experimental flag, this error can’t occur.
Fix 1: repair malformed markup
Open the named file in a code editor. SVGO parses SVG as XML, which is stricter than a browser. Look for:
- A bare
&in text or attributes. Write&. - Unclosed elements such as
<path d="...">with no/>or</path>. - Content before the root element, like a byte-order mark, stray whitespace and text, or a second XML declaration.
- Entity references defined in a DOCTYPE by older Illustrator exports (
&ns_svg;). Re-export as plain SVG.
A quick check: open the SVG directly in a browser. If it shows an XML parsing error, SVGO will fail on it too. Exporting again with “SVG 1.1” or “minify off” settings usually produces a clean file.
Fix 2: check the optimizer config
svgoOptimizer() accepts an SVGO config object. A typo in a plugin name or an invalid option can make every SVG fail, not just one:
// astro.config.mjs
import { defineConfig, svgoOptimizer } from 'astro/config';
export default defineConfig({
experimental: {
svgOptimizer: svgoOptimizer({
multipass: true,
plugins: [
'preset-default',
'removeDimensions',
],
}),
},
});
If every SVG fails after a config change, remove the options and test with svgoOptimizer() alone. Then add plugins back one at a time. SVGO’s plugin list has the valid names.
Fix 3: rule out the file
If you need the build to pass now, test whether one file is the problem:
- Comment out the import of the named SVG and rebuild.
- If the build passes, fix or re-export that file.
- As a stopgap, move the file to
public/and reference it with<img src="/logo.svg" alt="…" />. Files inpublic/aren’t processed, so they skip the optimizer.
Remember the stopgap loses inline rendering and currentColor styling, so fix the source file when you can.
After the fix
Rebuild with npm run build and compare the output size of the optimized SVG with the original to confirm the optimizer is doing useful work. The SVG components guide covers props and styling for imported SVGs, and the asset pipeline guide explains what Astro processes and what it copies as-is. Our project structure docs explain src/assets/ versus public/, as used across Komal Astro themes.
FAQ
Do I need to install SVGO separately?
No. Astro depends on SVGO and exports svgoOptimizer from astro/config. You only need to enable it under experimental.svgOptimizer.
Why does the SVG display in the browser but fail to optimize?
Browsers recover from many markup errors. SVGO parses the file as strict XML, so problems like an unescaped & that a browser forgives will still fail.
Can I turn off SVG optimization for one file?
The optimizer runs on imported SVGs. Serving a file from public/ with an <img> tag bypasses it, at the cost of inline rendering.