http.etag.matchesIfNoneMatch
v0.1.0 latestCheck if a current ETag matches an If-None-Match header value using weak comparison per RFC 9110 §13.1.2.
Check if a current ETag matches an If-None-Match header value using weak comparison per RFC 9110 §13.1.2.
Signature
function matchesIfNoneMatch(current: ETag, inm: IfNoneMatch): boolean
Type Definitions:
ETag— interfaceIfNoneMatch— type
Problem
Implementing 304 Not Modified requires correctly evaluating the If-None-Match header against the current resource's ETag. The evaluation must use weak comparison and handle the * wildcard, comma-separated lists, and mixed weak/strong tags.
How It Works
Uses weak comparison per RFC 9110 §13.1.2. { any: true } always matches. Tag lists are checked with weakEqual against the current ETag.
Boundaries
- For If-Match (which requires strong comparison), use strongEqual directly instead.
- Returns false for empty tag lists.
Replaces
Common boilerplate this function replaces:
inm.any || inm.tags.some(tag => tag.value === current.value)
Examples
http.etag.matchesIfNoneMatch({ weak: true, value: "abc" }, { any: true }); // true
http.etag.matchesIfNoneMatch({ weak: false, value: "abc" }, { tags: [{ weak: true, value: "abc" }] }); // true
http.etag.matchesIfNoneMatch({ weak: false, value: "abc" }, { tags: [{ weak: false, value: "xyz" }] }); // false
Standards
Caveats
- Uses weak comparison — a weak and strong ETag with the same value will match.
- This is correct for If-None-Match but NOT for If-Match.
FAQ
How to implement 304 Not Modified in JavaScript?
Parse the If-None-Match header with parseIfNoneMatch(), then check matchesIfNoneMatch(currentETag, parsed). If true, respond with 304.
Does matchesIfNoneMatch use weak or strong comparison?
Weak comparison, per RFC 9110 §13.1.2. This is correct for If-None-Match evaluation.
What does * mean in matchesIfNoneMatch?
{ any: true } always matches, regardless of the current ETag value.
Can I use matchesIfNoneMatch for If-Match?
No. If-Match requires strong comparison. Use strongEqual directly for that.
Does matchesIfNoneMatch handle mixed weak/strong tags?
Yes. Weak comparison ignores the W/ flag, so both weak and strong tags in the list are compared by value only.