This version is in beta. Some features may change before release.

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:

Code
rust
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.).

Info

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):

Code
http
GET / HTTP/1.1
Accept-Encoding: gzip, br
Code
http
HTTP/1.1 200 OK
Content-Encoding: gzip

A 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 + the collectstatic command gather and serve your assets.
  • umbral-storage for production static serving.
webcompressiongzipbrotliperformance