Gzip and Brotli Compression Explained
Before your server sends an HTML, CSS, or JavaScript file, it can squash it into a smaller package. The browser unpacks it on arrival. This is text compression, and it's one of the easiest speed wins, because text files compress very well and visitors download far less data.
The two main methods
Gzip has been the standard for years. It's supported everywhere and widely enabled by default.
Brotli is a newer algorithm that usually compresses text more tightly than gzip, at comparable decompression speed. All current major browsers support it over HTTPS.
Many servers offer both and use whichever the browser asks for. The browser tells the server what it supports in a header, and the server replies with a header saying what it used.
What to compress
Compress text-based files:
- HTML
- CSS
- JavaScript
- JSON and XML
- SVG images
- Web fonts in formats that aren't already compressed
- Plain text files
What not to compress
Don't try to compress files that are already compressed, such as JPEG, PNG, WebP, AVIF, MP4, and ZIP files. It wastes processing time and yields little. For these, optimize the files themselves. See how to compress images for the web and WebP vs AVIF vs JPEG.
How to check if it's on
- In your browser's developer tools, open the Network panel, select a CSS or JavaScript file, and look at the response headers for
content-encoding. Values includegzipandbr(Brotli) - Speed-testing tools usually warn if text compression is missing. See how to test website speed
- Online compression checkers can test a URL
How to turn it on
Managed hosts, CDNs, and website builders often enable compression automatically. Check your dashboard or settings. See what is a CDN.
Your own server can be configured to compress the file types listed above. The specific settings depend on the server software, and your host's documentation or support can help.
WordPress and other CMS sites may be helped by caching or performance plugins that enable compression, if your host doesn't. See how to speed up a WordPress site.
Pre-compressing and dynamic compression
Static files can be compressed once ahead of time at a high compression level and served as-is. Dynamic pages are compressed on the fly, where very high levels can add processing time, so moderate settings are usual. Good hosts and CDNs manage this for you.
Combine with other techniques
Compression works best alongside:
- Minifying your code, which removes unnecessary characters before compression
- Browser caching, so files aren't downloaded again
- HTTP/2 and HTTP/3
- Removing unused code altogether
A note on security
Compression has some theoretical security implications for secrets mixed with attacker-controlled content, but standard setups on typical small sites are widely used. Hosts and frameworks handle it appropriately. See how to keep your website secure.
The bottom line
Make sure your server compresses text files with Brotli or gzip. It's usually a setting, it costs nothing, and it makes every page lighter.