Repository navigation
Important: This release switches the package over to ESM and requires node >= 22.
Upgrading
Use the named fresh export and pass a response object with getHeader(name). A Node.js ServerResponse works directly.
import { fresh } from "fresh";
const isFresh = fresh(req.headers, res);If you store response headers in a plain object, pass { getHeader: (name) => responseHeaders[name] } as the second argument.
New APIs and behavior
preconditionFailed(reqHeaders, res)evaluatesIf-MatchandIf-Unmodified-Sincefor GET and HEAD requests that may require a 412.allowsRange(reqHeaders, res)checks whetherIf-Rangepermits processing a range request. It accepts an exact strong ETag match. Date matching requires{ lastModifiedIsStrong: true }.- A matching validator can produce a 304 Not Modified when the request sends
Cache-Control: no-cache. - ETag lists handle commas inside quoted tags and horizontal tabs between tags. Date validators support all three HTTP-date formats and reject inputs such as ISO 8601 timestamps.
Performance
This release is 1.4x faster for a single ETag match, 2.2x faster for a four-tag list, and 3.7x faster for an HTTP-date check.
| Freshness check | 2.0.0, prebuilt headers | 2.0.0, res.getHeaders() per call | 3.0.0, direct res |
|---|---|---|---|
| Single ETag match | 35.5 ns | 175.1 ns | 24.6 ns |
| Four ETags, match on the last tag | 158.6 ns | 300.9 ns | 73.4 ns |
| HTTP date, not modified | 366.6 ns | 510.4 ns | 97.9 ns |
It's even faster when you include the performance overhead of res.getHeaders().
Changed
- Treat HTAB as optional whitespace in If-None-Match / If-Match lists by @spokodev in #77
- Do not split If-None-Match entity-tags on commas inside quotes by @official-burak in #80
- Refactor to TypeScript and ESM by @blakeembrey in #83
- Improve the API shape for performance by @blakeembrey in #86
- Skip cache-control check by @blakeembrey in #87
- Improve perf for etag lists by @blakeembrey in #89
- Implement RFC date parser by @blakeembrey in #90
- Add precondition and allows range methods by @blakeembrey in #92
New Contributors
- @alxndrsn made their first contribution in #43
- @UlisesGascon made their first contribution in #45
- @Phillip9587 made their first contribution in #55
- @spokodev made their first contribution in #77
- @blakeembrey made their first contribution in #84
- @official-burak made their first contribution in #80
- @ghostkeni made their first contribution in #82
Full Changelog: v2.0.0...v3.0.0