DEV Community

啦啦啦种太阳
啦啦啦种太阳

Posted on

Your SVG optimizer silently broke your icon. Here's the one-line check.

Your SVG optimizer silently broke your icon. Here's the one-line check.

I spent an afternoon comparing SVG optimizers on a set of 28 real icons from Simple Icons. The sizes looked fine. Two files made the batch tool crash. Three of them produced a file that opened without complaint, rendered as a blank square, and reported a smaller byte count than the original.

The reason: the optimizer had replaced path coordinates with the literal string NaN.

This post is what I learned about the failure, how to detect it in one command, and why a preview can show you a perfect icon while the file you download is broken.


What NaN in a path means

A <path d="..."> is a list of drawing commands followed by numbers. Producing an optimized file means parsing those numbers, converting relative moves to absolute ones, rounding them, and printing them back. NaN ("not a number") is what a program writes when one of those steps produced something it could not compute.

NaN is not a valid coordinate. The browser stops drawing that part of the shape. Depending on where it lands you get a missing subpath, a warped outline, or nothing at all.

And here is the part that makes it dangerous: the file is still well-formed XML. No error, no warning, opens fine in any editor, passes any "is this valid XML" check. The only sign is the three letters.

Where it comes from: arc commands with joined flags

When I bisected the input, the trigger turned out to be a syntax detail, not the artwork.

An arc command (A absolute, a relative) takes seven numbers: two radii, a rotation, two single-digit flags, and a destination point. The SVG spec says those flags are single digits, and that a separator before them is optional. So both of these are valid and mean the same thing:

<path d="M2 2A1 1 0 01 0 2z"/>   <!-- flags written as 01 -->
<path d="M2 2A1 1 0 0 1 0 2z"/> <!-- flags written as 0 1 -->
Enter fullscreen mode Exit fullscreen mode

In the build I tested, the first form came out as:

a1 1 0 1 0 0 NaN
Enter fullscreen mode Exit fullscreen mode

A coordinate replaced by NaN. The second form was fine.

Two findings that saved me a lot of wrong conclusions:

  • It is not self-reproducing. Feeding the broken build's own output back in worked fine, because it writes flags with a space on the way out. So the corruption only hits input that arrived packed.
  • It is version-dependent. The same file fails in one optimizer version and succeeds in another, because the path parsing and number formatting code differs. A regression test against your actual tool is the only reliable answer.
Input form Result in the tested build What it means
A1 1 0 01 0 2 (flags joined) Coordinate replaced by NaN Check any file using this form
A1 1 0 0 1 0 2 (flags spaced) Correct output Still verify the drawing
Any file, another version Undetermined Version matters — test with your own tool

The spec reference: W3C SVG 1.1, elliptical arc commands.

Why the preview lied to me

The UI showed 1.62k → 1.45k ↓10.1% next to a perfectly drawn logo. Download the file and it renders as nothing.

The preview was built from the original markup, not the optimized output. So the image was a faithful rendering of my source file — displayed next to a byte count that referred to a different file. Two halves of the same interface describing two different files.

Even a preview built from the real output can miss it at the wrong scale. Icons are shown large in a tool and small on a page. A missing subpath inside a letter is invisible at 24px and obvious in a navbar at 400% zoom.

The one-line check

The defect is a literal string, so a text search finds it:

grep -c NaN icon.svg
# 0 on a clean file; anything above 0 means a broken path
Enter fullscreen mode Exit fullscreen mode

For a whole folder before you publish:

grep -rc NaN assets/
Enter fullscreen mode Exit fullscreen mode

PowerShell:

(Select-String -Path .\*.svg -Pattern NaN).Count
Enter fullscreen mode Exit fullscreen mode

No terminal? Open the file in an editor and search for NaN. One match is enough to reject it.

This catches this class of defect. It does not catch a coordinate rounded into a slightly different curve, an id removed while your CSS still targets it, or a viewBox dropped while the layout expects scaling. Those need a visual comparison. Do both.

Checklist before you replace an asset

Step What it catches
Search the output for NaN Coordinates the optimizer failed to compute
Compare drawing to source at final display size Changed curves, missing subpaths, lost detail
Confirm viewBox and dimensions survived Broken scaling in responsive layouts
Check ids your CSS or scripts reference Highlight, masking and animation silently stopping
Keep the original until the replacement is verified Lost source if the result is unusable

None of this takes long for one icon, and together it costs less than finding a broken logo in production. The size number is evidence about bytes, not about correctness.

If a file comes back broken

Keep the original, and record the optimizer version and settings. Try a different version, or turn off the path-rewriting pass — that is the pass most likely responsible. If the file came from an icon library, check whether a newer release exists; upstream fixes sometimes remove the syntax that triggers it.

Turning path rewriting off gives up most of the size reduction on path-heavy artwork, so treat it as a fallback, not a default.


I hit this while working on SVG Optimize, a free browser tool that reduces SVG markup locally without uploading the file. The bug was in the bundled optimizer, not in SVGO itself — current SVGO handled all 28 icons correctly. I replaced the bundle, added a guard that rejects any output containing NaN, and wrote up the full investigation with the arc-command repro at svg-optimize.com/svg-nan-path.

If you have a folder of icons you have not re-checked since the last time you optimized them:

grep -rl NaN path/to/icons/
Enter fullscreen mode Exit fullscreen mode

Any file it lists is worth a second look.

Top comments (0)