Optimize SVG for web
svg_web_optimizeWeb-optimize SVG by removing Inkscape editor elements, unused definitions, and rounding coordinates to reduce file size losslessly.
Instructions
Web-optimize an SVG: strip editor metadata, drop dead structure, reduce coordinate precision.
When to use: losslessly shrinking an SVG for the web (direct-DOM, non-destructive). To inspect
what WOULD be stripped first use quality_report; for lossy node reduction use simplify_path.
Key params: three reversible cleanups — (1) remove Inkscape/sodipodi editor-only elements,
namespaced attributes, and XML comments; (2) drop unreferenced <defs>, every unreferenced
id, and empty groups (referenced ids preserved so no #frag / url(#frag) / href breaks);
(3) round geometry numbers (path d, transforms, x/y/width/…) to precision decimals
(0-8, default 2; root viewBox untouched). keep_ids is an allowlist of ids that must NEVER be
stripped as "unreferenced" — pass a deliberate human/a11y id (e.g. one from rename_object) to
keep "one clean file with a stable id"; unknown ids are ignored. Re-running on
optimized output removes/rounds nothing further.
Return shape: WebOptimizeResult — the reversible-edit fields (operation_id, snapshot_id,
before/after preview) plus machine-diffable deltas bytes_before, bytes_after, and removed
(a {code: count} map keyed IDENTICALLY to quality_report.opportunities), so an agent on a
byte budget can compute the saving without parsing prose or stat-ing the file.
Example: svg_web_optimize(doc_id, precision=2, keep_ids=["header"])
Render and look before you trust this edit: render with render_preview (or live_render_view)
and inspect the result before relying on it; restore_snapshot reverts it if it is wrong.
Risk class: medium.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doc_id | Yes | ||
| keep_ids | No | ||
| precision | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doc_id | Yes | ||
| changed | Yes | ||
| removed | Yes | ||
| summary | No | ||
| bytes_after | Yes | ||
| snapshot_id | Yes | ||
| bytes_before | Yes | ||
| operation_id | Yes | ||
| preview_after | No | ||
| preview_before | No |