Skip to content

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

Merged
Eomm merged 1 commit into
fastify:mainfrom
TalhaHunter101:fix/cache-control-on-304
Nov 14, 2026
Merged

Eomm merged 1 commit into
fastify:mainfrom
TalhaHunter101:fix/cache-control-on-304

Conversation

@TalhaHunter101

Copy link
Copy Markdown
Contributor

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>

@Eomm Eomm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. RFC 9110 requires a 304 to carry the Cache-Control

@Eomm
Eomm merged commit d619e35 into fastify:main Nov 14, 2026
17 checks passed
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

2 participants