compressResponse
Summary
Compresses a Response based on the client's Accept-Encoding header.
Compression is skipped for:
- Requests with no Accept-Encoding header (RFC 7231)
- Empty responses
- Already compressed responses
- Responses with Content-Length below threshold (default: 1024 bytes)
- Responses with Cache-Control: no-transform
- Responses advertising range support (Accept-Ranges: bytes)
- Partial content responses (206 status)
Responses eligible for content-coding negotiation include Accept-Encoding in
the Vary header, even when identity is selected or the request does not include
Accept-Encoding.
When compressing, this function:
- Sets Content-Encoding header
- Removes Content-Length header
- Sets Accept-Ranges to 'none'
- Converts strong ETags to weak ETags (per RFC 7232)
Signature
function compressResponse(
response: Response,
request: Request,
options: CompressResponseOptions,
): Promise<Response>
Parameters
response
The response to compress
request
The request (needed to check Accept-Encoding header)
options
Optional compression settings
Returns
A response with the negotiated content coding and cache metadata