The Lab's app.js is 199.9 KB. On a 1.6 Mbps connection, that file takes about 1 second just to travel. With a little server setup, only 37.7 KB travels, without changing a single character of the code.
Compression means the server shrinks text files (HTML, CSS, JavaScript, JSON, SVG) before sending them, and the browser puts them back to full size as soon as they arrive. The content is exactly the same, only far less goes over the network. It's like packing clothes in a vacuum bag: the same clothes, a much more compact suitcase, and once opened at the destination they're back to normal size. But you can't vacuum a brick: files that are already compressed, like photos and video, can't be shrunk again.
Browser and server agree through headers
The browser says which compressions it can open with the request header Accept-Encoding. The server picks one, compresses, and names its choice in the response header Content-Encoding:
GET /assets/app.js HTTP/2
accept-encoding: gzip, deflate, br, zstd
HTTP/2 200
content-type: text/javascript
content-encoding: br
vary: Accept-Encoding
content-length: 37673
accept-encoding: gzip, deflate, br, zstdis what Chrome sends over HTTPS.brmeans Brotli. Over plainhttp://, the browser only offersgzip, deflate.content-encoding: brmeans the response body is Brotli-compressed. No such header means it was sent as is.vary: Accept-Encodingtells caches (CDNs, proxies) that the response depends on that header, so a browser that can't do Brotli is never sent the Brotli version.content-length: 37673is the size that crosses the network: 37,673 bytes, while the original file is 199,850 bytes.
You don't write any code in the page: the browser opens the compression automatically. What you set up is the server.
How much smaller?
These are the files in the Lab, really measured with gzip level 6 and Brotli level 11 (1 KB = 1,000 bytes):
| File | Original | gzip | Brotli |
|---|---|---|---|
app.js |
199.9 KB | 46.7 KB (76.6% smaller) | 37.7 KB (81.1% smaller) |
style.css |
47.3 KB | 8.9 KB (81.2%) | 7.9 KB (83.4%) |
index.html |
49.4 KB | 14.0 KB (71.6%) | 11.8 KB (76.1%) |
hero.webp |
98.9 KB | 98.5 KB (0.4%) | 98.3 KB (0.6%) |
app.js becomes 46.7 KB with gzip. Text compresses easily because it's full of repetition: function, const, class names, the same HTML tags over and over. In general, text files shrink by 60 to 80 percent, about 70 percent on average; JavaScript that's already minified often shrinks by about 70 to 80 percent.
On a 1.6 Mbps network (about 200 KB per second), app.js takes 1 second sent as is, and only 0.19 seconds with Brotli. One thing doesn't change: once it arrives, the browser opens the compression, so the JavaScript engine still parses and runs 199.9 KB. Compression saves network, not CPU work on the user's phone.
gzip or Brotli
- gzip has been around since the 1990s and is supported by every browser and server.
- Brotli (
br), made by Google, produces files about 10 to 25 percent smaller than gzip for text. In the Lab,app.jsis 37.7 KB with Brotli against 46.7 KB with gzip: almost 20 percent smaller. - The price: Brotli at its highest level (11) is much slower to compress. On one laptop,
app.jstakes about 245 ms with Brotli 11, and only about 4 ms with gzip level 6. Opening it in the browser is fast for both, under 1 ms. - Every modern browser supports Brotli over HTTPS. Set up both: Brotli for browsers that offer it, gzip as the fallback. zstd, which is newer, is also starting to be offered by Chrome and Firefox.
Static or on-the-fly
There are two moments to compress:
- Static, at build time. Files whose content doesn't change (
app.js,style.css) are compressed once at deploy, saved asapp.js.brandapp.js.gz, and the server just sends the matching one. Because it happens once, use the highest level: Brotli 11. - On-the-fly, every request. Responses made on the spot, for example JSON from an API, are compressed every time they're sent. Use a medium level, for example Brotli 4 to 5 or gzip 6, so the server doesn't get busy compressing.
An example in Nginx (the Brotli module is installed separately):
gzip on;
gzip_types text/css text/javascript application/javascript application/json image/svg+xml;
gzip_min_length 1000; # files under 1,000 bytes are not compressed
gzip_static on; # send app.js.gz if it already exists
brotli on;
brotli_comp_level 5; # on-the-fly: a medium level
brotli_types text/css text/javascript application/javascript application/json image/svg+xml;
brotli_static on; # send the app.js.br from the build if it exists
text/html doesn't need to be listed: Nginx always compresses HTML once gzip on is set. Many modern hosts (Cloudflare, Vercel, Netlify) already compress automatically.
What doesn't need compressing
- Images, video, audio: JPEG, PNG, WebP, AVIF, MP4 are already compressed inside their format. In the Lab,
hero.webponly saves 0.4 percent, and server CPU is wasted. - WOFF2 fonts: they already use Brotli inside. In the Lab,
font.woff2actually grows by 23 bytes with gzip. - Very small files: gzip adds a header and trailer of about 18 bytes. A
product.jsonholding 1 product, 116 bytes, becomes 123 bytes after gzip. That's why there's a minimum likegzip_min_length 1000.
🎯 Check it in DevTools
Open the Network tab, reload the page, then click a JS file. Under Response Headers, look for content-encoding: it should be br or gzip. In the file list, the Size column shows two numbers if you turn on "Big request rows": the top one is the size that crossed the network, the bottom one the original size. If the two are almost the same for a text file, compression isn't on yet.
⚠️ What often goes wrong when setting up compression
- Compressing images and fonts. There's nothing to gain, sometimes they even grow, and server CPU is wasted. Limit compression to text types.
- Brotli level 11 on-the-fly. Every request waits hundreds of milliseconds just to be compressed. Level 11 is for the build, a medium level for dynamic responses.
- Forgetting
Vary: Accept-Encoding. A CDN can store the Brotli version and send it to a client that can't open it. - Thinking compression reduces the browser's work. The size that's parsed is still the original size. To reduce the JavaScript itself, use minifying, tree shaking and code splitting.
TL;DR: Compression shrinks text files before they're sent. The browser offers through Accept-Encoding (Chrome over HTTPS: gzip, deflate, br, zstd), the server answers with Content-Encoding and Vary: Accept-Encoding. Text usually shrinks by 60 to 80 percent, about 70 percent on average: in the Lab app.js goes from 199.9 KB to 46.7 KB (gzip) or 37.7 KB (Brotli). Brotli is about 10 to 25 percent smaller than gzip but much slower to compress at level 11, so that level is used at build time; dynamic responses are compressed on-the-fly at a medium level. Don't compress images, video, WOFF2 or files under about 1 KB. Compression saves network, not parse work.
⚡ Agreeing through headers
Write:
accept-encoding: gzip, deflate, br, zstd
content-encoding: br
vary: Accept-Encoding
Meaning: The browser offers the compressions it can open through Accept-Encoding, and the server names its choice in Content-Encoding. You write no code in the page: what you set up is the server.
⚡ Text shrinks a lot
Meaning: Text is full of repetition, so it usually shrinks by 60 to 80 percent. In the Lab, app.js goes from 199.9 KB to 46.7 KB with gzip. But the browser still parses 199.9 KB: compression saves network, not CPU work.
⚡ gzip or Brotli
Guess first: what is the most important difference between gzip and Brotli?
Brotli's output is about 10 to 25 percent smaller, but it is much slower to compress at level 11. That's why level 11 is used at build time, and dynamic responses are compressed at a medium level.
Meaning: Images, video, WOFF2 and files under about 1 KB don't need compressing: they save next to nothing, and can even grow.