WEB-46Recommended
Compress every text response
Compress every text response with Brotli (or zstd), with gzip as a fallback, ideally ahead of time at the highest level.
How to do it
Compress static text files at build time (Brotli 11 and gzip 9) and serve the right one by Accept-Encoding. Compress each rendered page once and keep the result. Compress the rest on the fly at a moderate level. Name the encoding header as Say which request headers change a response says.
Examples
Shell example
# at build time
brotli -q 11 -k dist/*.html dist/*.css dist/*.js
gzip -9 -k dist/*.html dist/*.css dist/*.js
nginx example
brotli_static on; # needs the ngx_brotli module
gzip_static on;
gzip on;
gzip_comp_level 5;
gzip_types text/css application/javascript application/json image/svg+xml text/markdown; # text/html is always on
How to check
curl -sI -H 'Accept-Encoding: br' shows Content-Encoding: br for HTML, CSS and JS. With Accept-Encoding: gzip the same files come back as gzip.
Measured
On a reference site, turning compression off took Lighthouse mobile Performance from 99 to 92 (from 99 to 89 on a second page). It took LCP from 2.1 to 3.3 s (from 2.1 to 3.75 s on the second page). It took page weight from 202 to 403 KB. On those two pages, it was the largest single effect of every feature tested.
Sources
- RFC 9110 – Accept-Encoding
- RFC 7932 – Brotli Compressed Data Format
- RFC 8878 – Zstandard Compression
Related rules
- WEB-43 Say which request headers change a response in Website
- WEB-56 Use shared dictionaries for big bundles in Website
- WEB-160 Send more in the first round trip in Website
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/performance/compression">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.