Response compression
Opt-in gzip/brotli compression for responses, chosen from the client's Accept-Encoding.
Response compression
Enable gzip/brotli compression with one builder call:
App::builder() // ... .compression() .build()?This wraps the router in a tower-http CompressionLayer. It picks the algorithm from the request's Accept-Encoding (brotli or gzip), and skips bodies that are already encoded or aren't worth compressing (images, video, etc.).
Compression is off by default. In most deployments a reverse proxy (nginx, a CDN) already compresses, and doing it twice is wasted CPU. Turn it on when you serve directly, a single binary with no proxy in front.
What gets compressed
A text response (HTML, JSON, CSS, JS, SVG) is compressed when the client sends Accept-Encoding: gzip (or br):
GET / HTTP/1.1Accept-Encoding: gzip, brHTTP/1.1 200 OKContent-Encoding: gzipA client that doesn't advertise an encoding gets the plain body back. The layer never compresses blindly.
See also
- Static files: the
{% static "path" %}tag + thecollectstaticcommand gather and serve your assets. umbral-storagefor production static serving.