Optimize draw.io SVG exports
An SVG exported from diagrams.net carries a lot that a picture does not need — rasterised label fallbacks, editor bookkeeping, and layers of empty groups. Removing them typically takes a 500 KB export to around 22 KB, and 354 KB gzipped to under 3 KB.
Everything runs in this browser. Your diagram is never uploaded, and there is no server holding a copy of it.
Loading…
What it does
- Drop rasterised label fallbacks
- When draw.io cannot guarantee a fallback font will render — notably for CJK text — it rasterises the label and embeds it as a base64 PNG. On a typical export those images are 91% of the file, and around 99% of the achievable gzip win, because already-compressed data is something gzip cannot touch. This single pass is most of the value. An <image> anywhere else is real diagram content and is never touched.
- Collapse <switch> onto draw.io’s own text
- Where the export already carries a faithful <text> fallback, the HTML branch is dropped and the text hoisted at its exact position. The text-equivalence check is a hard precondition rather than a formality: draw.io truncates the fallback with an ellipsis when a label overflows its shape, and collapsing onto that would silently corrupt the label.
- Convert HTML labels to SVG text
- The only way to remove a <foreignObject> from an export that has no text fallback — and the only way to make those labels render outside a browser at all. In this app the shaping is done by your browser’s own layout engine rather than a reimplementation, and the result is checked against draw.io’s own coordinates.
- Collapse the group scaffolding
- draw.io wraps every shape in several groups that carry nothing. Removing them is asserted to be geometrically lossless, not merely to look right: every path’s absolute bounding box is resolved through its transforms and required to match exactly.
- Strip editor bookkeeping and unused ids
- data-cell-id attributes, empty style elements, the redundant transparent-background declaration. Referenced ids are collected from url(), href, ARIA attributes, animation timing and CSS before anything is removed — a reference that lives only in an @supports rule is still a reference.
- Sanitize
- These files usually come from someone else, so scripts, event handlers, external references, SMIL animation and CSS url() are removed by default. Two different risks are being prevented: that the document makes this tool fetch something, and that the output turns a static picture into a beacon that leaks a viewer’s IP and referrer.
The passes and the reasoning behind each are documented in full in the drawio-svg-optimizer repository, which is also available as a command-line tool.