brotli¶
Format: .br · Algorithm: LZ77 + Huffman + context modelling + a built-in static dictionary · First released: 2013 (fonts), 2015 (general) by Google · Spec: RFC 7932
Brotli was designed for the web. Its built-in dictionary of common HTML, CSS, JavaScript and English text gives it an edge on small text files, which is exactly what web pages are made of. All modern browsers accept Content-Encoding: br. Outside the web it's uncommon; for files and backups use zstd.
Basics¶
brotli file # -> file.br, keeps file
brotli -d file.br # decompress
brotli -q 11 file # max quality (0-11, default 11)
brotli -q 5 file # faster, typical for on-the-fly HTTP
brotli -c file > out.br # to stdout
brotli -t file.br # test
brotli --rm file # remove source
Pre-compressing static assets¶
For static sites, compress once at build time with maximum effort and let the web server serve the pre-compressed files:
find public/ -type f \( -name '*.html' -o -name '*.css' -o -name '*.js' -o -name '*.svg' -o -name '*.json' \) \
-exec brotli -q 11 -k {} \;
# gzip fallback for older clients
find public/ -type f \( -name '*.html' -o -name '*.css' -o -name '*.js' \) \
-exec gzip -9 -k -n {} \;
nginx¶
Brotli needs the ngx_brotli module (packaged by some distros, or built in to some nginx distributions):
brotli on;
brotli_comp_level 5;
brotli_types text/plain text/css application/json application/javascript image/svg+xml;
brotli_static on; # serve .br files when present
Use a moderate level for on-the-fly compression; level 11 is too slow per request.
Brotli vs. gzip vs. zstd on the web¶
| Encoding | Support | Notes |
|---|---|---|
gzip |
Universal | Safe fallback |
br |
All modern browsers (HTTPS) | Best ratio for text, especially pre-compressed |
zstd |
Newer browsers | Fast on-the-fly compression at good ratios |