GrepBoard/Manuals
605 manuals ·

Shrink and Check AI-Generated SVG Files with svgo

·Tested on Ubuntu 24.04 · Node.js 24.22.0 · svgo 4.1.0·Test scope ↓·7 steps

Shrink an AI-generated SVG with svgo, compare sizes before and after, then grep for a missing viewBox and for script or event attributes before publishing.

Ask an AI for a logo or icon as SVG and you get comments, editor namespaces, a <metadata> block and coordinates with three decimals. Shrink the file, then use grep to see what survives instead of reading the markup by eye.

The test file icon-a.svg below is a hand-made sample that imitates common AI output (comments, a DOCTYPE, a <style> with classes, an unused gradient, style attributes). It is not real generated output, so treat the percentages as a rough guide.

[1] Install svgo inside the project and check the version.

console
$ npm install svgo$ npx svgo --version4.1.0

[2] Optimize with the defaults. Without -o, svgo overwrites the input, so give an output path.

console
$ wc -c icon-a.svg1763 icon-a.svg$ npx svgo -i icon-a.svg -o out/a.svgicon-a.svg:Done in 86 ms!1.722 KiB - 69.9% = 0.519 KiB

out/a.svg is 531 bytes. The comments, XML declaration, DOCTYPE, <metadata>, editor namespaces, the unused <linearGradient> and the .000 decimals are gone, and the path went from M 256.000 64.000 … to M256 64c64 64 …. After gzip the sizes are 873 → 344 bytes.

[3] Check what is still there after the same run.

Left inNote
<title> · <desc>In the --show-plugins list, removeTitle has no (preset-default) mark. <desc> stayed even though removeDesc is in the default preset.
style="…"The <style> classes moved onto the elements as attributes. The count of style= went from 2 to 4.
<script> · onload · onclickSee [4].

To turn style attributes into plain attributes (fill="…"), enable convertStyleToAttrs. If you do not need <title> for accessibility, enable removeTitle too.

[4] Run a file that contains scripts. icon-c.svg has an onload, a <script>, a javascript: link and an onclick.

console
$ npx svgo -q -i icon-c.svg -o out/c.svg$ cat out/c.svg<svg xmlns="http://www.w3.org/2000/svg" onload="fetch('https://example.com/ping')" viewBox="0 0 100 100"><script type="text/javascript">document.title = &apos;x&apos;;</script><a><path fill="#ef4444" d="M0 0h100v100H0z" onclick="alert(2)"/></a></svg>

The default config only dropped the href that started with javascript:. The <script>, onload and onclick stayed. svgo shrinks files; it is not a checker.

[5] Add removeScripts and removeDimensions in a config file.

js
// svgo.config.mjsexport default {  plugins: [    'preset-default',    'removeScripts',    'removeDimensions',  ],};
console
$ npx svgo --config svgo.config.mjs -q -i icon-c.svg -o out/c2.svg$ cat out/c2.svg<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><a><path fill="#ef4444" d="M0 0h100v100H0z"/></a></svg>

The file dropped from 250 to 117 bytes, with the <script> and event attributes gone. Still, do not rely on this plugin to sanitize SVG from outside sources; the test covered one sample.

removeDimensions deletes width and height, and when there is no viewBox it builds one from those two values. Checked on a 200×200 icon-b.svg:

console
$ npx svgo --config svgo.config.mjs -q -i icon-b.svg -o out/b2.svg$ grep -o '<svg[^>]*>' out/b2.svg<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">

[6] Review with grep before you upload. Look for files with no viewBox, and for files with script-like strings.

console
$ grep -L viewBox *.svgicon-b.svg$ grep -lE '<script|\son[a-z]+=|javascript:' *.svgicon-c.svg

-L prints the names of files with no matching line. Of the three originals in the test folder, only icon-b.svg had no viewBox. The default svgo config does not add one (out/b.svg still has none), so if a file turns up here, use removeDimensions from [5] or write viewBox="0 0 200 200" by hand.

[7] Last, make sure the optimized file is not broken. svgo reports an error and exits with code 1 on an unclosed tag.

console
$ npx svgo -q -i bad.svg -o out/bad.svgSvgoParserError: bad.svg:2:0: Unclosed root tag

An SVG that was cut off mid-generation fails at this step. This check cannot tell you whether the shapes draw correctly, so open the file in a browser once.

#svg#svgo#web