Serving compressed SVG files
Tags: https, svg, webdev
Published: 2017-12-27 14:53:42

---
title: "Serving compressed SVG files"
published: true
tags: https,svg,web-development
canonical_url: https://www.moxio.com/blog/6/serving-compressed-svg-files
---

![](https://cdn-images-1.medium.com/max/811/0*wHh61Ifp5OlcT_-p.png)

Over the years, Scalable Vector Graphics (SVG) has become the _de facto_ standard for vector images on the web. One of its disadvantages however is its verbose XML-based format, with relatively large file sizes as a result. Luckily, the plaintext nature of XML lends itself well to compression. The SVG standard reserves the&nbsp;.svgz-extension for gzip-compressed SVG files, which can typically achieve [about 20–50%](http://calendar.perfplanet.com/2014/tips-for-optimising-svg-delivery-for-the-web/) of the original file size. When not actively editing them, saving SVG files as svgz makes sense: it saves disk space, the compression is lossless and it spares the time needed for on-the-fly compression when serving them over the web. Serving compressed SVG files correctly from a web server takes some care&nbsp;however.

### HTTP headers for&nbsp;.svgz-files

SVG viewers conforming to the standards support svgz-files, as long as they are served with the [correct](https://www.w3.org/TR/SVG11/conform.html#ConformingSVGServers) HTTP&nbsp;headers:

```
Content-Type: image/svg+xml
Content-Encoding: gzip
```

It may seem strange that both&nbsp;.svg (plaintext SVG) and&nbsp;.svgz (compressed SVG) files are served with the same MIME type. The difference is in the [Content-Encoding](https://tools.ietf.org/html/rfc7231#section-3.1.2.2) header, which combined with the Content-Type describes the type of content being served. In this case they distinguish between ‘SVG, plain’ and ‘SVG, gzipped’, i.e. a readable XML format or a bunch of binary gibberish.

### Content-Encoding vs. Transfer-Encoding

There also exists an HTTP/1.1 header [Transfer-Encoding](https://tools.ietf.org/html/rfc7230#section-3.3.1), which looks similar to the Content-Encoding, but is actually fundamentally different. The Content-Encoding indicates the ‘natural’ encoding of the content (in which it is stored and handled), whereas the Transfer-Encoding only describes a temporary encoding applied for (faster or more reliable) transport. A client that receives an HTTP response&nbsp;with

```
Content-Type: application/sql
Transfer-Encoding: gzip
```

should thus decompress the content and offer plaintext SQL to the user. However, when the response headers&nbsp;are

```
Content-Type: application/sql
Content-Encoding: gzip
```

the server actually meant to present gzip-compressed SQL. The client should not decompress such content and probably offer to save the binary data as a&nbsp;.sql.gz-file. For&nbsp;.svgz files, this means that the client does not immediately decompress the content when received, but rather pass the compressed byte stream to the SVG viewer (which will then do the decoding&nbsp;itself).

Part of the confusion stems from the fact that transfer encoding is an HTTP/1.1 concept and browser support for gzip transfer encoding (as should be indicated in a [TE-header](https://tools.ietf.org/html/rfc7230#section-4.3)) is very limited. As a consequence, people looking for an efficient way to transfer their JavaScript, CSS and HTML started to serve these files with a compressed body and a Content-Encoding header (which was already present in HTTP/1.0) to indicate the encoding used. Browsers happily contributed to the widespread propagation of this kludge by decompressing this content, even when downloading to disk, as users surely did not expect to find binary gibberish on their disks when saving the source of a thus compressed JavaScript file. This leaves us in a state of widespread misuse of the Content-Encoding header as a substitute for Transfer-Encoding that will probably not be fixed in HTTP 1&nbsp;anymore.

### Serving compressed files for&nbsp;download

This poses a problem when serving&nbsp;.svgz-files for download (with Content-Disposition: attachment), e.g. to a user who clicks a download button in a filemanager-like application. In such a case, the user probably wants to end up with the compressed (binary) content of the&nbsp;.svgz-file on disk, not with a plaintext file with a&nbsp;.svgz-extension. However, due to the abuse of the Content-Encoding header described above, users expect other compressed file types to be saved in readable plaintext, so clients have a hard time to decide what to do. In an attempt to escape from this squeeze, most browsers implement some kind of guessing logic involving the extension of the downloaded file. This logic [does](https://bugzilla.mozilla.org/show_bug.cgi?id=233047#c23) [not](https://bugs.chromium.org/p/chromium/issues/detail?id=83292) [always](https://bugzilla.mozilla.org/show_bug.cgi?id=406948) [yield](https://bugzilla.mozilla.org/show_bug.cgi?id=986924) the desired result however, especially when there is a custom filename parameter in the Content-Disposition header.

To work around these problems, we should prevent serving content that has a ‘natural’ compression with a Content-Encoding header if the file has an attachment Content-Disposition. Just leaving the Content-Encoding header out for&nbsp;.svgz files is not an option, as it would make it look as if we're serving plaintext SVG. A simple solution is to serve these files (only when downloaded) with a Content-Type of application/octet-stream ("Arbitrary binary data; just save these bytes to disk, please") and no Content-Encoding:

```
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="image.svgz"
```

When serving&nbsp;.svgz files for inline purposes (display in a viewer) we still send the recommended headers to allow correct rendering:

```
Content-Type: image/svg+xml
Content-Encoding: gzip
Content-Disposition: inline
```

### Conclusions

- Saving and serving SVG content as&nbsp;.svgz-files is worthwhile, but takes some&nbsp;care
- Know the difference between Content-Encoding and Transfer-Encoding
- Serving naturally-compressed content for download with a Content-Encoding header is unreliable, serve it as a non-encoded octet-stream instead

_Originally published at_ [_www.moxio.com_](https://www.moxio.com/blog/6/serving-compressed-svg-files)_._

* * *