Skip to main content

Import

Usage

Options

The compress middleware accepts an optional CompressionOptions object:
'gzip' | 'deflate'
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.
number
default:1024
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-Encoding header
    • Response already has Transfer-Encoding header
    • Request method is HEAD
    • Content length is below threshold
    • Content type is not compressible
    • Response has Cache-Control: no-transform
  • Removes Content-Length header after compression (length changes)
  • Sets Content-Encoding header to the compression method used
  • Uses native CompressionStream API for compression
  • Automatically selects encoding based on Accept-Encoding header
  • Only compresses compressible content types (text/html, application/json, etc.)