Skip to main content

Compress Middleware

Compress Responses Before Sending to Clients The Compress middleware supports Gzip, Brotli, and Zstandard compression. The activation of compression and the compression method choice rely on the request’s Accept-Encoding header.

Configuration Examples

Enable Compression

Compression with Excluded Content Types

Compression with Specific Encodings

When Compression Occurs

Responses are compressed when all these criteria are met:
  1. The Accept-Encoding request header contains gzip, br, zstd, and/or * (with or without quality values)
  2. The response is not already compressed (no Content-Encoding header)
  3. The response Content-Type is not excluded (or is included if using includedContentTypes)
  4. The response body is larger than the minimum configured bytes (default: 1024)
If the Accept-Encoding header is absent and no defaultEncoding is configured, the response won’t be compressed. If present but empty, compression is disabled.

Configuration Options

array
List of content types to exclude from compression. Compared case-insensitively with whitespace ignored.
The excludedContentTypes and includedContentTypes options are mutually exclusive.
gRPC (application/grpc) is never compressed.
array
List of content types to include for compression. Only responses with these content types will be compressed.
The excludedContentTypes and includedContentTypes options are mutually exclusive.
integer
default:"1024"
Minimum response body size in bytes required for compression.
When data is sent to the client on flush (e.g., Transfer-Encoding: chunked), this configuration is ignored and data is compressed immediately.
string
Default encoding if the Accept-Encoding header is not present or contains a wildcard (*).No fallback occurs when the header value is empty or unsupported.
array
default:"gzip, br, zstd"
List of supported compression encodings. Valid entries are gzip, br (Brotli), and zstd (Zstandard).The order sets priority - the top entry has the highest priority.

Usage Examples

Compress Specific Content Types

Prioritize Zstandard Compression

Combining Compression with Other Middlewares