GioJSdocs
On this page

[compression]

Brotli and gzip compression of responses, negotiated from Accept-Encoding.

gio.toml
[compression]
enabled = true
min_size_bytes = 1024
prefer_brotli = true

Reference

KeyDefaultDescription
enabledbooleantrueCompress responses for clients that accept it. Turn it off when a proxy or CDN in front compresses: compressing twice only costs CPU.0 / false / empty: Every response is sent uncompressed
min_size_bytesinteger1024Responses with a known length below this many bytes are sent as-is - compressing them costs more than it saves. Streamed responses have no known length and are always compressed. At most 65535; a larger value is a startup error.
prefer_brotlibooleantruetrue: Brotli for clients that accept it, gzip otherwise. false: gzip only, which costs less CPU per response (every client that accepts Brotli also accepts gzip).0 / false / empty: gzip only

Behavior

  • Brotli (br) and gzip are the only encodings; a client that accepts neither gets the plain body.
  • Never compressed: images (image/*), server-sent events (text/event-stream), gRPC, responses that already carry a Content-Encoding, and partial (Content-Range) responses. A route.ts returning fetch(upstream) does not count: the body fetch() decoded is sent without the upstream's encoding and compressed here.
  • Every response the layer could compress carries Vary: accept-encoding, so caches keep one copy per encoding. With enabled = false no response gets it.
  • Compression is the outermost response step: it runs after the security headers, CSP nonces and <html lang> have been written into the body.

Startup logs response compression disabled ([compression] enabled = false) when it is off. No key in this section logs a warning.

Examples

Behind a compressing CDN

gio.toml
[compression]
enabled = false

Save CPU on a small instance

gio.toml
[compression]
prefer_brotli = false       # gzip is cheaper to produce
min_size_bytes = 4096

Good to know

  • A cached page's weak ETag stands for all of its encodings, and a 304 keeps the Vary its 200 would carry.
  • While CSP nonces are on, a route handler must not compress its own body: a response with its own Content-Encoding is refused with 500. Leave compression to the server.

Version history

VersionChanges
v0.1.0-beta.8Introduced as a gio.toml section: enabled, min_size_bytes and prefer_brotli are honored.
v0.1.0-beta.1Brotli and gzip compression of responses of 1 KB and more, not configurable.