Skip to content

fix: evaluate If-None-Match like Express - #177

Open
lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/match-express-freshness
Open

lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/match-express-freshness

Conversation

@lprnmns

@lprnmns lprnmns commented Oct 23, 2026

Copy link
Copy Markdown

In #116 the 304 handling was described as matching Express' etag middleware. In practice it differs in a few places. With fastify.register(etag) and a route that returns 'hello' (also mounted as a POST route, and as a GET route that sets reply.code(404)), compared with Express' default ETag and req.fresh:

request @fastify/etag Express
GET, If-None-Match: <tag> 304 304
GET, If-None-Match: "x", <tag> 200 304
GET, If-None-Match: * 200 304
POST, If-None-Match: <tag> 304 200
GET on the 404 route, If-None-Match: <tag> 304 404

The hook compared the header to the ETag as a single string, and it did so for any method and any status code. Express' req.fresh only considers GET/HEAD with a 2xx (or 304) response, and fresh treats If-None-Match as a list of entity tags (plus *) with the weak comparison from RFC 9110 §13.1.2. The last two rows are the more visible ones: a POST, or an error response whose body happens to hash to the client's cached tag, is turned into an empty 304.

This adds an isFresh() helper with those same conditions. The existing weak/strong combinations behave as before, because both sides go through the same W/ stripping.

Tests: five cases in test/generic.js (a match inside a list, a list with no match, *, POST, a 404 route), run for each algorithm. 25 of them fail without the change. npm test (100% coverage, tstyche) and npm run lint pass. I also updated the README sentence that describes when a 304 is sent.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant