top | item 20680559

Minify Your SVGs

264 points| vzhou842 | 6 years ago |victorzhou.com | reply

65 comments

order
[+] sheetjs|6 years ago|reply
Unfortunately the reference to the actual minifier, SVGO (https://github.com/svg/svgo), is buried in the middle of the post.

SVGO is an incredible project driven by a single volunteer in spare time (originally Kir Belevich https://twitter.com/deepsweet , and for the last few years Lev Solntsev https://twitter.com/ruGreLI). It's the type of project you probably use regularly behind another tool like Sketch or SVGOMG (https://jakearchibald.github.io/svgomg/). They accept donations (https://www.paypal.me/deepsweet) and you should definitely consider contributing if you find it useful.

[+] social_quotient|6 years ago|reply
Pro tip for people with SVGs that have an image inside. You can pull that base64 image out of the svg, convert to image, compress it (tinypng, pngcrush etc) then convert back to base64 and out back in the SVG. We automated this with a slackbot but it’s often overlooked with all SVG optimization scripts.
[+] mhe|6 years ago|reply
Somewhat related: a while ago realized that the SVGs that draw.io generates (exports) have an interesting property. If you include an SVG inside such a drawing and you export it to SVG, that SVG is actually embedded as base64. This is unfortunately not supported by some programs (e.g., Microsoft Office). A quick fix is to actually expand the SVG into the big SVG. I wrote a quick tool for this that does this for you (I should consider open sourcing this maybe). Anyway, I bet doing this and then minimizing the SVG could result in more savings?
[+] v33ra|6 years ago|reply
[+] pier25|6 years ago|reply
I use it every time I'm using SVG for the web.

My only problem is that when pasting SVG directly from Illustrator it finds an invalid character at the end and refuses to use it. I have to paste the SVG into an editor, remove some invisible character at the end, and then paste to svgomg.

I reported this and offered to PR but never got an answer: https://github.com/jakearchibald/svgomg/issues/145

[+] bastawhiz|6 years ago|reply
Hardly a day goes by that I don't use svgomg. If you need a one-off SVG minified, this is the tool for you.
[+] pixelbath|6 years ago|reply
I used this tool just the other day; very comprehensive, and very fantastic.
[+] discreditable|6 years ago|reply
Be aware that svgo is a lossy operation. One thing it does is round the digits in numbers to a certain precision. When I last worked with it, it also did some things I did not like such as mess with viewboxes. I found that it messed up the sizing of SVGs embedded with <img> tags in certain browsers. It has been a while since I used it though.
[+] gregable|6 years ago|reply
I was curious about this. It looks like it has numerous individual plugins that provide the actual functionality. Do you know if some of the plugins are lossless, while others lossy? It may be possible to pick and choose the lossless ones.
[+] spiralganglion|6 years ago|reply
I absolutely love ImageOptim for manually optimizing SVGs, JPGs, PNGs, and GIFs. It's a simple GUI that wraps some excellent compression libs (including SVGO), which I find much faster to use for one-off image optimization than the CLI. It does a clever job of trying multiple compressors and picking whichever one creates the smallest file for each image. You can run it in a lossless or lossy mode, and tune various compression options. It's free (gratis), OSS, and runs on Mac, Windows, and Linux.

I make it a habit of running images through ImageOptim before sending them via Messages or Slack, as a courtesy for people on slow connections.

https://imageoptim.com (by HN user pornel)

Bonus round: When it comes to optimizing SVGs, nothing beats hand-editing the SVG source. I've done a fair bit of that optimizing the artwork for https://www.lunchboxsessions.com and it's been totally worth the effort. Taking a detailed 10k SVG down to 500 bytes with no perceptual difference is a huge win when you have hundreds of those SVGs on the page and you serve people who still use dialup.

[+] uxamanda|6 years ago|reply
ImageOptim also strips out a lot of (all?) the image metadata which is often a place people leak data unintentionally.
[+] pelagic_sky|6 years ago|reply
I've been using ImageOptim for a while now. Saves me so much trouble when I get junky SVG's as well as exporting my own.
[+] taeric|6 years ago|reply
Would be more compelling with the sizes of that neutral net in the different passes. As it is, this is like comparing two algorithms on a small dataset without accounting for growth in data on behavior.
[+] jkoudys|6 years ago|reply
A related (but less automated) advantage is if you're building react components that render SVG, this minified output will be much shorter and hopefully easier to reach through and figure out where you want to insert your own colours/child components.
[+] iamben|6 years ago|reply
Illustrator has an export SVG function (as opposed to just saving as an SVG, which I was doing and then using a minifer for a long time) that will minify it for you.

That said, it currently appears unable to reopen those minified SVGs (go figure).

[+] pier25|6 years ago|reply
FYI you can copy any vector in Illustrator and when you paste it somewhere it is actually SVG code.
[+] burevestnik|6 years ago|reply
I love seeing efforts to make software more efficient, so I enjoyed this post, but at the same time your own blog has a 434kb 512x512 png favicon. I'm not at all a web developer and so I don't know if there is a technical reason for that, but it seems a bit absurd in juxtaposition to your goal to reduce a 2kb svg to 100 bytes.

I've seen this on a number of other blogs, which seem to focus on minimal design. Can anyone explain what the need is for these enormous favicons?

[+] Nadya|6 years ago|reply
512x512 is specifically for browser loading splash screens when the site is added as a launcher on a phone. It's required for Chrome to have an "Add to Home Screen" prompt and requires a 192x192 and 512x512 icon. I imagine most minimalist sites you browse are expecting people to add the website as an icon/app launcher on their phones. Not sure how common an experience that is for users, I sure as hell wouldn't but maybe other people might.

Was intended for web apps I believe but is usable by sites that aren't web apps.

Read more here: https://developers.google.com/web/fundamentals/web-app-manif...

[+] jchw|6 years ago|reply
Don’t forget to also use GZip or another transport compression on them as well. Text tends to compress well, and can net additional benefits on top of minification.
[+] jrockway|6 years ago|reply
I ran the examples in the article through gzip. The long one is 789 bytes compressed, and the short one is 266 bytes compressed.

The reality is that while text compresses well, gzip doesn't know what data is relevant. It keeps it all. So for this example, the original long svg contains things like inkscape configuration (the window height, the current layer name, etc.) while the shorter one omits all that. So not emitting that information will always be more efficient than emitting that information and compressing it.

[+] willis936|6 years ago|reply
This is neat, but I wonder: are there path optimization tools? For instance: flattening, removal of entirely occluded paths, and combination of paths.
[+] notduncansmith|6 years ago|reply
Not exactly that but comes close (and perhaps could be used for that if you rasterized the input vector): https://vectormagic.com/

I haven’t used their desktop edition but on the web it worked really well for cleaning up images. I imagine similar algorithms could be applied directly to SVGs to simplify them.

Worth noting is the manual step in their workflow - I think there’s only so much you can do before you need a human to decide whether any important meaning has been lost.

[+] willis936|6 years ago|reply
SVG optimization seems like a good choice for delivery, but oftentimes I find myself using SVG in a development context. Having human readable, annotated SVG files is useful when looking through previous revisions. In terms of compressed filesize: there's no serious practical gain.
[+] lotyrin|6 years ago|reply
It's analogous to JS minification -- Clients on the production site should get a bare minimum and the VCS should have the readable form with all the digits of precision and whatever comments, non-functional groups acting as layers in the editing software, etc.
[+] jgalt212|6 years ago|reply
Aren't garden variety SVG files ~75% smaller than other lossless compression methods (e.g. PNG). As such, is minification necessary or a best practice?

In short, is this an intellectual exercise or something that should be in an SVG producer's toolkit?

[+] chrismorgan|6 years ago|reply
Raster formats have a worst-case file size that is linked to their visual size.

SVG has no such limit; for a given visual size it can have arbitrarily many objects. If you’re not careful, you can quite easily end up with multi-megabytes SVGs where a PNG would have been <100KB.

SVG is therefore a dangerous format to use if you don’t know what you’re doing, because its performance characteristics are quite unlike raster formats.

[+] mtlynch|6 years ago|reply
This is an excellent post!

It does a fantastic job of defining the problem and walking the reader through the process of solving it. The language is so succinct and clear that even if the reader had never heard of SVGs before, they would understand this post.

Well done, Victor!

[+] pcmaffey|6 years ago|reply
Nice work, svgo is awesome. I'd love to adapt this to a non-gatsby post-build tool that also runs image compression on any jpg/png files.

Big advantage to running post-build besides convenience is that sometimes Inkscape doesn't render optimized svg's correctly. So I have to keep originals elsewhere.

[+] adityapatadia|6 years ago|reply
We provide hosted service for optimising SVG. If anyone is interested for image optimisation as a service, take a look here: https://www.gumlet.com
[+] jarek-foksa|6 years ago|reply
Just another shameless plug here. I'm developing an SVG editor which focuses on generating clean and readable SVG files (e.g. no redundant namespaces, metadata or deeply nested structures).

You can try it online on https://boxy-svg.com, there are also desktop apps for macOS, Windows, Linux and Chrome OS.

[+] ricardobeat|6 years ago|reply
For inlining, you can even go further and remove that xmlns attribute to end with something around 50 bytes. I'd recommend keeping the viewbox though, since it's required for proper resizing.
[+] kuschku|6 years ago|reply
Most of the time, automated optimizers for SVG are relatively bad, though, and rewriting the SVG by hand can get a 2KiB svgo result down to 200 bytes or less.