Compression
Negotiate zstd, brotli, gzip or deflate response compression from Accept-Encoding with one HttpConfig flag.
One flag makes the transport compress responses when the client asks for
it. With HttpConfig.compression on, each request’s Accept-Encoding picks
zstd, brotli, gzip or deflate, weighed as RFC 9110 §12.5.3 reads it, and the
response body is encoded with
async-compression before it leaves the
process. Off by default — in
most deployments a reverse proxy already does this.
HttpModule::for_root(HttpConfig { compression: true, ..Default::default()}),Wire it in
Section titled “Wire it in”Like every HttpConfig field, the flag is dual-path — pin it in code as
above, or set it in the environment. The demo’s api pins no HttpConfig: its
flag sits in its own .env beside its port, and its chart sets it for a
deployment:
NESTRS_HTTP__COMPRESSION=trueA client that advertises support gets an encoded body; one that does not gets plain bytes — the same route, negotiated per request:
$ curl -sS -H 'Accept-Encoding: gzip' -D - -o /dev/null localhost:3002/api-jsonHTTP/1.1 200 OKcontent-type: application/jsoncontent-encoding: gzip
$ curl -sS -D - -o /dev/null localhost:3002/api-jsonHTTP/1.1 200 OKcontent-type: application/jsonWhat it negotiates
Section titled “What it negotiates”- The coding weighed highest wins,
zstdfirst among equals;gzip;q=0refuses gzip, and anidentityweighed above every coding is answered unencoded. A client sending noAccept-Encodinggets plain bytes. - Every response the layer could have encoded carries
Vary: Accept-Encoding, so a shared cache keeps the encodings apart. - An encoded body is another representation’s bytes, so a strong
ETagthe handler set is sent weak (W/"…", RFC 9110 §8.8.3).If-None-Matchcompares weakly, so the tag still revalidates, and a304carries the tag and theVaryits encoded200would. - A request body whose
Content-Encodingnames one of the four codings is decoded before the route reads it, still under the transport’s body cap.
Limits
Section titled “Limits”- The layer sits inside CORS, so a preflight (
OPTIONS, no body) is never compressed, and outside the handler, so what the handler returns is what gets encoded. - An event stream is never compressed: an encoder would hold each event back
until its buffer filled. Neither is a
204, a304, nor a response already carryingContent-Encoding. - A partial answer — a
206, or any response carryingContent-Range— is sent as written: its range counts bytes before any encoding (RFC 9110 §14.4), so an encoded part would name offsets its body does not have. - A weak tag never matches
If-MatchorIf-Range, which compare strongly (RFC 9110 §13.1.1, §13.1.5): a write guarded by the tag of an encoded read is refused, and a resumed download restarts whole. A client guarding a write reads the tag from an unencoded answer, sent withoutAccept-Encoding. - Turn it off (the default) when a proxy, CDN, or load balancer in front of the app compresses — doing it twice only burns CPU.
Going further
Section titled “Going further”- Configuration — every
HttpConfigfield. - Streaming responses — SSE and streamed bodies.