Agent Wranglers

Performance Required

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.

Example

# at build time
brotli -q 11 -k dist/*.html dist/*.css dist/*.js
gzip -9 -k dist/*.html dist/*.css dist/*.js
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.

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.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers website playbook: https://agentwranglers.se/playbook/website/performance/compression.