Import
Usage
Options
Theetag middleware accepts an optional ETagOptions object:
boolean
default:false
Define using weak validation. If
true, then W/ is added to the prefix of the ETag value.string[]
The headers that you want to retain in the 304 Not Modified response. By default, includes headers required by the HTTP spec for 304 responses.
(body: Uint8Array) => ArrayBuffer | Promise<ArrayBuffer>
A custom digest generation function. By default, it uses SHA-1 algorithm via
crypto.subtle.digest. This function is called with the response body as a Uint8Array and should return a hash as an ArrayBuffer or a Promise of one.Signature
Constants
RETAINED_304_HEADERS
Examples
Basic usage
Weak validation
Custom retained headers
Custom digest function
Apply to specific routes
Behavior
- Generates ETag header from response body using SHA-1 hash (by default)
- If response already has an ETag header, uses it instead of generating a new one
- Compares ETag with
If-None-Matchheader from request - Returns 304 Not Modified if ETags match
- 304 responses only include retained headers (specified by spec or config)
- Weak validation uses
W/prefix for ETags - If digest cannot be generated (e.g., no body), skips ETag generation
- If
crypto.subtleis not available and no customgenerateDigestis provided, skips ETag generation