Import
Usage
Options
Thecompress middleware accepts an optional CompressionOptions object:
The compression scheme to allow for response compression. If not defined, both
gzip and deflate are allowed and will be used based on the Accept-Encoding header. gzip is prioritized if this option is not provided and the client accepts both.The minimum size in bytes to compress. Responses smaller than this threshold will not be compressed. Defaults to 1024 bytes (1KB).
Signature
Examples
Basic usage
Force gzip compression
Force deflate compression
Custom threshold
Disable compression for small responses
Apply to specific routes
Behavior
- Skips compression if:
- Response already has
Content-Encodingheader - Response already has
Transfer-Encodingheader - Request method is
HEAD - Content length is below threshold
- Content type is not compressible
- Response has
Cache-Control: no-transform
- Response already has
- Removes
Content-Lengthheader after compression (length changes) - Sets
Content-Encodingheader to the compression method used - Uses native
CompressionStreamAPI for compression - Automatically selects encoding based on
Accept-Encodingheader - Only compresses compressible content types (text/html, application/json, etc.)