Skip to content

v3.0.0

Latest

Choose a tag to compare

@blakeembrey blakeembrey released this 02 Dec 23:02
388c73e

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) evaluates If-Match and If-Unmodified-Since for GET and HEAD requests that may require a 412.
  • allowsRange(reqHeaders, res) checks whether If-Range permits 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

New Contributors

Full Changelog: v2.0.0...v3.0.0