Before you start

This guide uses the official SVGO 4.1.0 release and its v4 plugin defaults. The guide is for trusted design assets, not for sanitizing outside SVG uploads.

What you’ll make
A separate optimized SVG, byte sizes and a structural accessibility check.
What you’ll need
Node.js LTS, npm, Python 3, Bash and one trusted SVG or the included vector fixture.

Step by step

  1. Create the baseline

    Keep the original in the fixture folder and make a separate output. The sample uses a title referenced by aria-labelledby and a viewBox so its geometry scales.

  2. Run SVGO locally

    Pin the CLI package and use an explicit allowlist of three documented plugins: removeComments, cleanupAttrs and sortAttrs. This deliberately skips transforms that can discard IDs, title, viewBox or accessibility attributes.

  3. Validate the output

    Parse both SVG documents and verify viewBox, role, aria-labelledby, title text and gradient reference. Byte savings are only useful if those remain correct.

  4. Review how it is embedded

    An inline SVG uses the accessible name in the SVG, while an img element needs its own alt in the host HTML when the image conveys meaning. If repeating the same inline asset, ensure IDs are unique per document. Compare at two sizes and on the intended backgrounds.

Commands

export npm_config_cache="$PWD/.npm-cache"
npm init -y
npm install --save-dev --save-exact --ignore-scripts svgo@4.1.0
npx --no-install svgo --version
cat > source.svg <<'EOF'
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-labelledby="icon-name">
  <title id="icon-name">Playback ready</title>
  <!-- Export annotation; not part of the art. -->
  <defs><linearGradient id="brand-fill"><stop stop-color="#61362c"/><stop offset="1" stop-color="#b86651"/></linearGradient></defs>
  <rect x="0" y="0" width="64" height="64" rx="11" fill="url(#brand-fill)"/>
  <path fill="#fff" d="M25 19v26l20-13z"/>
</svg>
EOF
cat > svgo.config.mjs <<'EOF'
export default {plugins:['removeComments','cleanupAttrs','sortAttrs']};
EOF
npx --no-install svgo --config svgo.config.mjs -i source.svg -o optimized.svg
python3 - <<'EOF'
from pathlib import Path
import xml.etree.ElementTree as E
a,b=(E.parse(f).getroot() for f in ('source.svg','optimized.svg'))
for k in ('viewBox','role','aria-labelledby'):
    assert a.attrib[k]==b.attrib[k],k
ns='{http://www.w3.org/2000/svg}'
name=b.find(ns+'title')
assert name.get('id')==b.get('aria-labelledby') and name.text=='Playback ready'
assert any(x.get('id')=='brand-fill' for x in b.iter())
assert any(x.get('fill')=='url(#brand-fill)' for x in b.iter())
for f in ('source.svg','optimized.svg'):
    print(f,Path(f).stat().st_size,'bytes')
EOF

Sign-in & permissions

No account, API key, or external upload. A public dependency download may be needed. Pause before a system install; do not work around a restriction.

No agent skill, plugin, or MCP connector required.

Prompt for your agent

Use with: Codex, Claude Code, or another agent with local terminal and file access. Commands use Bash or a compatible shell.

Copy the whole prompt into a supported agent. After any manual step, tell the same agent to continue from that step.

Use a clean project folder for this workflow: Shrink a scalable SVG without losing its accessible name. Supported setup: Codex, Claude Code, or another agent with local terminal and file access. Commands use Bash or a compatible shell.. Start from the original documentation: https://svgo.dev/docs/usage/.

1. SET UP
Start in a new project folder and check the listed tools. Use the pinned package or official platform build, no global upgrades. Check each command against its official source and stop on missing permissions or capabilities.

Documented commands or code reference (respect the comments and replace placeholders before running individual commands):
export npm_config_cache="$PWD/.npm-cache"
npm init -y
npm install --save-dev --save-exact --ignore-scripts svgo@4.1.0
npx --no-install svgo --version
cat > source.svg <<'EOF'
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-labelledby="icon-name">
  <title id="icon-name">Playback ready</title>
  <!-- Export annotation; not part of the art. -->
  <defs><linearGradient id="brand-fill"><stop stop-color="#61362c"/><stop offset="1" stop-color="#b86651"/></linearGradient></defs>
  <rect x="0" y="0" width="64" height="64" rx="11" fill="url(#brand-fill)"/>
  <path fill="#fff" d="M25 19v26l20-13z"/>
</svg>
EOF
cat > svgo.config.mjs <<'EOF'
export default {plugins:['removeComments','cleanupAttrs','sortAttrs']};
EOF
npx --no-install svgo --config svgo.config.mjs -i source.svg -o optimized.svg
python3 - <<'EOF'
from pathlib import Path
import xml.etree.ElementTree as E
a,b=(E.parse(f).getroot() for f in ('source.svg','optimized.svg'))
for k in ('viewBox','role','aria-labelledby'):
    assert a.attrib[k]==b.attrib[k],k
ns='{http://www.w3.org/2000/svg}'
name=b.find(ns+'title')
assert name.get('id')==b.get('aria-labelledby') and name.text=='Playback ready'
assert any(x.get('id')=='brand-fill' for x in b.iter())
assert any(x.get('fill')=='url(#brand-fill)' for x in b.iter())
for f in ('source.svg','optimized.svg'):
    print(f,Path(f).stat().st_size,'bytes')
EOF

2. PAUSE FOR ME IF NEEDED
For a host install, sign-in, permission screen, or model download that needs my consent, stop and tell me the exact app, control and what will be authorized. Do not enter passwords, OTPs or secrets in chat. Resume after I confirm.

3. BUILD THE FIRST RESULT
Create the fixture and optimized copy. Run the structural checks and report before/after bytes. If I supply a different trusted icon, adapt the checks to protect its own title, IDs, viewBox and references. Keep both files and visually compare at 24 and 96 CSS pixels if the host supports it. If the result is not smaller, keep the original; do not turn on riskier plugins silently.

4. CHECK THE RESULT
Do not accept the optimized file if its accessible name, sizing, or referenced paints change. Byte savings vary with the source. Give me the actual file paths and anything that still needs a manual action. Do not publish or make a purchase.

Sources