Compression Middleware
Response compression middleware that negotiatesbr, gzip, and deflate from Accept-Encoding and applies sensible defaults for when compression is useful.
Installation
Function
compression()
Creates middleware that automatically compresses responses based on the client’s Accept-Encoding header and content type.
Signature:
options- Optional compression settings
Options
CompressionOptions
threshold
Minimum size in bytes to compress (only enforced if Content-Length present).
- Type:
number - Default:
1024
filterMediaType
Optional filter to control which responses get compressed based on media type.
- Type:
(mediaType: string) => boolean - Default: Uses
isCompressibleMimeTypefrom@remix-run/mime
encodings
Which encodings the server supports for negotiation in order of preference.
- Type:
Encoding[] | ((response: Response) => Encoding[]) - Default:
['br', 'gzip', 'deflate'] - Values:
'br'|'gzip'|'deflate'
zlib
Node.js zlib options for gzip/deflate compression.
- Type:
ZlibOptions | ((response: Response) => ZlibOptions) - Default: Node.js defaults
- See: Node.js zlib options
brotli
Node.js zlib options for Brotli compression.
- Type:
BrotliOptions | ((response: Response) => BrotliOptions) - Default: Node.js defaults
- See: Node.js Brotli options
Basic Usage
- The client supports compression (
Accept-Encodingheader with a supported encoding) - The response is large enough (≥1024 bytes if
Content-Lengthis present) - The response hasn’t already been compressed
- The response doesn’t advertise range support (
Accept-Ranges: bytes)
Custom Threshold
Custom Encodings
Dynamic Encodings
Custom Media Type Filter
Compression Options
Dynamic Compression Options
Related
- fetch-router - Router for the web Fetch API
- mime - MIME type utilities
- response - Response helpers