Skip to content

fix: apply configured Cache-control header on 304 responses - #205

Open
TalhaHunter101 wants to merge 1 commit into
fastify:mainfrom
TalhaHunter101:fix/cache-control-on-304
Open

TalhaHunter101 wants to merge 1 commit into
fastify:mainfrom
TalhaHunter101:fix/cache-control-on-304

Conversation

@TalhaHunter101

Copy link
Copy Markdown

Fixes #204

Summary

#192 changed cachingSetCacheControlHeader to apply the configured Cache-control directive only for statusCode >= 200 && statusCode < 300, setting no-store for all other status codes. As a result, 304 Not Modified responses—including conditional ETag cache hits handled internally by cachingLoadByEtag as well as explicit reply.code(304).send() responses—incorrectly received Cache-control: no-store.

Per RFC 9110 §15.4.5 and RFC 9111 §4.3.4, a 304 response must carry the same Cache-Control directive that a 200 OK response would have included so caches can retain and refresh stored freshness policies.

Changes

  • Updated cachingSetCacheControlHeader in index.js to apply the configured Cache-control value when (reply.statusCode >= 200 && reply.statusCode < 300) || reply.statusCode === 304.
  • Added unit tests in test/headers.test.js covering both explicit reply.code(304).send() responses and conditional 304 hits served via If-None-Match ETag caching.
  • Verified with npm test (26/26 unit tests with 100% c8 coverage + tstyche TypeScript tests) and npm run lint.

Checklist

Signed-off-by: Talha Shafeeq <talha.shafeeq.ts@gmail.com>

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.

304 Not Modified responses receive Cache-Control: no-store instead of configured caching directives

1 participant