1 | Mini SVG `data:` URI
|
2 | ====================
|
3 |
|
4 | This tool converts SVGs into the most compact, compressible `data:` URI that SVG-supporting browsers tolerate. The results look like this (169 bytes):
|
5 |
|
6 | ```url
|
7 | data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 50 50'
|
8 | %3e%3cpath d='M22 38V51L32 32l19-19v12C44 26 43 10 38 0 52 15 49 39 22 38z'/%3e
|
9 | %3c/svg%3e
|
10 | ```
|
11 |
|
12 | Compare to the Base64 version (210 bytes):
|
13 |
|
14 | ```url
|
15 | data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIH
|
16 | ZpZXdCb3g9IjAgMCA1MCA1MCI+PHBhdGggZD0iTTIyIDM4VjUxTDMyIDMybDE5LTE5djEyQzQ0IDI2ID
|
17 | QzIDEwIDM4IDAgNTIgMTUgNDkgMzkgMjIgMzh6Ii8+PC9zdmc+
|
18 | ```
|
19 |
|
20 | Or the URL-encoded version other tools produce (256 bytes):
|
21 |
|
22 | ```url
|
23 | data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%
|
24 | 2F2000%2Fsvg%22%20viewBox%3D%220%200%2050%2050%22%3E%3Cpath%20d%3D%22M22%2038V51
|
25 | L32%2032l19-19v12C44%2026%2043%2010%2038%200%2052%2015%2049%2039%2022%2038z%22%2
|
26 | F%3E%3C%2Fsvg%3E
|
27 | ```
|
28 |
|
29 | For a more realistic example, I inlined the icons from the [Open Iconic](https://useiconic.com/open) project into CSS files with the 3 above methods:
|
30 |
|
31 | | Compression | Base64 | Basic %-encoding | `mini-svg-data-uri` |
|
32 | |-------------|----------:|-----------------:|--------------------:|
|
33 | | None | 96.459 kB | 103.268 kB | 76.583 kB |
|
34 | | `gzip -9` | 17.902 kB | 13.780 kB | 12.974 kB |
|
35 | | `brotli -Z` | 15.797 kB | 11.693 kB | 10.976 kB |
|
36 |
|
37 | Roughly 6% smaller compressed, but don't write off the ≈20% uncompressed savings either. [Some browser caches decompress before store](https://blogs.msdn.microsoft.com/ieinternals/2014/10/21/compressing-the-web/), and parsing time/memory usage scale linearly with uncompressed filesize.
|
38 |
|
39 |
|
40 | Usage
|
41 | -----
|
42 |
|
43 | ```js
|
44 | var svgToMiniDataURI = require('mini-svg-data-uri');
|
45 |
|
46 | var svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M22 38V51L32 32l19-19v12C44 26 43 10 38 0 52 15 49 39 22 38z"/></svg>';
|
47 |
|
48 | var optimizedSVGDataURI = svgToMiniDataURI(svg);
|
49 | // "data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 50 50'%3e%3cpath d='M22 38V51L32 32l19-19v12C44 26 43 10 38 0 52 15 49 39 22 38z'/%3e%3c/svg%3e"
|
50 | ```
|
51 |
|
52 | You can also [try it in your browser at RunKit](https://npm.runkit.com/mini-svg-data-uri).
|
53 |
|
54 | ### CLI
|
55 |
|
56 | If you have it installed globally, or as some kind of dependency inside your project’s directory:
|
57 |
|
58 | ```sh
|
59 | mini-svg-data-uri file.svg # writes to stdout
|
60 | mini-svg-data-uri file.svg file.svg.uri # writes to the given output filename
|
61 | ```
|
62 |
|
63 | Use `--help` for more info.
|
64 |
|
65 | ### Warning
|
66 |
|
67 | * This **does not optimize the SVG source file**. You’ll want [svgo](https://github.com/svg/svgo) or its brother [SVGOMG](https://jakearchibald.github.io/svgomg/) for that.
|
68 |
|
69 | * The default output **does not work inside `srcset` attributes**. Use the `.toSrcset` method for that:
|
70 |
|
71 | ```js
|
72 | var srcsetExample = html`
|
73 | <picture>
|
74 | <source srcset="${svgToMiniDataURI.toSrcset(svg)}">
|
75 | <img src="${svgToMiniDataURI(svg)}">
|
76 | </picture>`;
|
77 | ```
|
78 |
|
79 | * The resulting Data URI should be wrapped with double quotes: `url("…")`, `<img src="…">`, etc.
|
80 |
|
81 | * This might change or break SVGs that use `"` in character data, like inside `<text>` or `aria-label` or something. Try curly quotes (`“”`) or `"` instead.
|
82 |
|
83 |
|
84 | FAQ
|
85 | ---
|
86 |
|
87 | ### Don’t you need a `charset` in the MIME Type?
|
88 |
|
89 | `charset` does nothing for Data URIs. The URI can only be the encoding of its parent file — it’s included in it!
|
90 |
|
91 | ### Why lowercase the URL-encoded hex pairs?
|
92 |
|
93 | It compresses slightly better. No, really. Using the same files from earlier:
|
94 |
|
95 | | Compression | Uppercase (`%AF`) | Lowercase (`%af`) |
|
96 | |-------------|------------------:|------------------:|
|
97 | | `gzip -9` | 12.978 kB | 12.974 kB |
|
98 | | `brotli -Z` | 10.988 kB | 10.976 kB |
|
99 |
|
100 | I did say *slightly*.
|
101 |
|
102 |
|
103 | Browser support
|
104 | ---------------
|
105 |
|
106 | * Internet Explorer 9 and up, including Edge
|
107 | * Firefox, Safari, Chrome, whatever else uses their engines
|
108 | * Android WebKit 3+
|
109 | * Opera Mini’s server-side Presto
|
110 |
|
\ | No newline at end of file |