Kest

Search

Search the documentation

HTTP

Compression

Send smaller responses so pages load faster and bandwidth costs less.

Text (JSON, HTML, CSS) shrinks to a fraction of its size with gzip, so it travels faster. The browser says it can decompress (Accept-Encoding: gzip), the server compresses, the browser decompresses; nothing changes in your code.

app.http(adapter) { useCompression(minBytes = 1024) }

Responses of at least minBytes are gzip-compressed for clients that send Accept-Encoding: gzip; smaller ones are sent as they are (compressing tiny bodies costs more than it saves). Content-Encoding and Vary are set for you. Only text-like content types are compressed: images, video and already-compressed files (.zip, .png) gain nothing and are left alone.

A 200 KB JSON list typically becomes 20 to 40 KB.

Compress in one place only

In production you can instead compress at the load balancer or reverse proxy (nginx, a CDN, an ingress), which is usually faster and also handles static assets. Do one or the other, not both: double compression wastes CPU and can break responses.

  • Kest in front of Next.js (Next.js): compress once, in Kest or at the edge, not in both.
  • Streaming responses and server-sent events must not be buffered for compression: skip them, or compress at the proxy with flushing enabled.

Trade-offs

Compression costs CPU. It is almost always worth it for text responses over the network; it is rarely worth it between services in the same data centre. If CPU is your bottleneck, measure before enabling.

Read more