Most servers compress responses as they send them. That works, but it forces a trade-off: Brotli’s highest level gives the smallest files and is far too slow to run on every request, so on-the-fly compression settles for a lower level.
A static file changes only when you deploy. Compress it once, at the highest level, and the server only has to pick the right file.
What It Saves
These are this website’s own files, measured on the build that serves this page:
| File | Original | gzip -9 | Brotli 11 |
|---|---|---|---|
| Home page HTML | 61,784 bytes | 11,093 bytes | 8,703 bytes |
| Largest JavaScript bundle | 553,448 bytes | 136,031 bytes | 111,698 bytes |
Brotli is about 20% smaller than gzip on both, and both are a fraction of the original. The server spends no CPU on compression at all.
Compress at Build Time
Compress text formats only: HTML, CSS, JavaScript, SVG, JSON and XML. Images, fonts in WOFF2 and video are compressed already. Files under 1 KB gain little.
find dist -type f -size +1k \
\( -name '*.html' -o -name '*.css' -o -name '*.js' -o -name '*.svg' \
-o -name '*.json' -o -name '*.xml' \) \
-exec gzip -k -9 {} \; -exec brotli -k -q 11 {} \;
Each page.html gets a page.html.gz and a page.html.br next to it. -k keeps the original for clients that accept neither.
If your build runs on Node.js, zlib.brotliCompress with BROTLI_PARAM_QUALITY: 11 does the same without the command-line tools. This site does it in a small build step.
Serve the Right File
The browser lists what it accepts in Accept-Encoding. The server looks for a matching file and sends it with Content-Encoding:
nginx
gzip_static on;
gzip_vary on;
# with the ngx_brotli module installed:
brotli_static on;
Caddy
file_server {
precompressed br gzip
}
Rust, with tower-http
let files = ServeDir::new("dist").precompressed_br().precompressed_gzip();
Two details decide whether this works in practice:
Vary: Accept-Encoding. It tells caches that the response depends on that header. Without it, a CDN can store the Brotli version and send it to a client that cannot read it.- The cache policy. Files with a content hash in their name never change, so they can be cached for a year:
Cache-Control: public, max-age=31536000, immutable. HTML keeps its name between deployments and must be revalidated:Cache-Control: public, max-age=0, must-revalidate.
Check It
Ask for Brotli and look at the response headers:
curl -sI -H 'Accept-Encoding: br' https://example.com/ | grep -iE 'content-encoding|vary'
content-encoding: br
vary: accept-encoding
Then compare how many bytes travel for each encoding:
for e in br gzip identity; do
curl -s -H "Accept-Encoding: $e" -o /dev/null -w "$e %{size_download}\n" https://example.com/
done
If br and identity print the same number, the server is not finding the compressed files. The usual causes are a build step that ran before the files were copied, or a deployment that left them out.
When a CDN Sits in Front
A CDN may compress again, or decompress and recompress, between its edge and the visitor. Check the response as a visitor receives it, through the CDN, and check it again after changing CDN settings. The origin’s files still help: the CDN fetches less, and visitors who reach a fresh edge get the small file sooner.