Performance and accessibility 4 min read Updated
Text compression with Brotli and gzip
In short
Serve HTML, CSS, JavaScript and SVG compressed with Brotli or gzip, and check the Content-Encoding header on real responses.
Your server sends full-size files when it could send smaller ones. Compression shrinks them, so pages arrive sooner, and the browser displays exactly the same content. Start with text files: HTML, CSS, JavaScript and SVG. Leave images and video alone, because most of those formats are already compressed.
Savings depend on the file
Compression lowers the transfer size of a response, but the saving varies by file. The MDN guide to compression in HTTP reports size reductions of up to 70% for some documents. Small files gain little, and files that are already compressed gain almost nothing. MDN describes gzip as the most common algorithm and Brotli as the newer challenger.
Compressing HTML can also help Largest Contentful Paint, because the browser finds the stylesheets and scripts of the page sooner. The web.dev article on text compression makes this point. For the metric itself, read the article on fixing Largest Contentful Paint.
Brotli and gzip side by side
Modern browsers advertise support for both algorithms in the Accept-Encoding header, so a server can offer both and let the browser choose. The web.dev article recommends Brotli wherever possible, and gzip as the fallback. In its comparison table, Brotli produced a smaller output than gzip for every test file. For angular-1.8.3.js the reduction was 81% with Brotli (256 KiB), against 76% with gzip (329 KiB).
Which files to compress
Compress text-based resources. The web.dev article names HTML, CSS, JavaScript and SVG as the usual candidates. Images, audio and video are different. Most image formats are already compressed, so compressing them again yields little or nothing. The MDN guide adds that the extra processing can make such a file larger, because the overhead costs more than the gain.
- Compress: HTML, CSS, JavaScript and SVG.
- Leave alone: JPEG, PNG, WebP, AVIF, audio and video, which are already compressed.
- Check: very small responses. Compression can make them larger, so servers often set a minimum size, which web.dev says is typically under four or five kilobytes.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Levels and processing time
Gzip levels run from 1 to 9, and Brotli levels from 0 to 11. The highest level gives the best ratio but takes the most time. For responses compressed on the fly, web.dev suggests settings in the middle of each range, which give the best trade-off between ratio and speed. Static files, compressed once before deployment, can use the most aggressive settings, because that work is done once instead of on every request.
How the browser and the server agree
The browser sends an Accept-Encoding header that lists the algorithms it supports, in order of preference. The server chooses one, compresses the body and names the choice in the Content-Encoding header of the response. The MDN guide says that when the server sends different representations of one URL, it must also send a Vary header that includes Accept-Encoding, so that caches can store each representation separately.
| Header | Sent by | What it tells the other side |
|---|---|---|
| Accept-Encoding | Browser | The compression algorithms it supports, in order of preference |
| Content-Encoding | Server | The algorithm used for this response body |
| Vary | Server | That the response depends on Accept-Encoding, so caches keep each version apart |
Caching rules decide how long a compressed response stays in a cache. They are covered in the article on HTTP caching.
A checklist for your server
Work through these steps after any change to the server configuration:
- Find the module or setting your server uses. The MDN guide names mod_deflate for Apache, the gzip module for Nginx and the httpCompression element for IIS.
- Request an HTML file, a CSS file and a JavaScript file, and read the response headers. Compressed responses should carry Content-Encoding with br or gzip, and the HTML response should also include Vary with Accept-Encoding.
- Open the Network panel in DevTools, reload the page and compare the transfer size with the resource size. A transfer size clearly smaller than the resource size shows that compression works.
- Measure a few files before and after the change, and keep the setting only if the saving is real. Recording the sizes in a performance budget helps you notice when they grow again.
This article summarises the MDN guide Compression in HTTP and the web.dev article on text compression. Savings depend on the content, and server settings differ between hosts, so check your own host’s documentation before you change anything.
Common questions
- Should I use Brotli or gzip?
- Use Brotli where your server supports it and keep gzip as a fallback. The web.dev article recommends Brotli wherever it is possible.
- Is it worth compressing images?
- Usually not. Images, audio and video are normally compressed already, and compressing them again tends to add overhead without a useful saving.
- How do I know that compression works?
- Open the Network panel in DevTools, reload the page and compare the transfer size with the resource size. A transfer size clearly below the resource size shows that compression is active.
Sources
- Compression in HTTP, MDN Web Docs. Accessed 24 November 2026
- Optimize the encoding and transfer size of text-based assets, web.dev. Accessed 24 November 2026
Further reading
-
How HTTP caching decides what is reused
Caching is safe to use when each response carries an explicit Cache-Control header that matches how often its content changes.
-
Fixing Largest Contentful Paint one delay at a time
Split the LCP delay into four parts, then fix the largest one first instead of guessing at image tweaks.
-
Choosing an image format for the web
Use JPEG for photographs, PNG for transparency and exact detail, SVG for icons and diagrams, and WebP or AVIF with a fallback where they help.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea