Skip to content

fix: do not cache non-2xx responses - #192

Merged
mcollina merged 2 commits into
fastify:mainfrom
imharjot:fix/no-cache-error-responses
Jul 11, 2026
Merged

mcollina merged 2 commits into
fastify:mainfrom
imharjot:fix/no-cache-error-responses

Conversation

@imharjot

Copy link
Copy Markdown
Contributor

The Cache-control header was applied in an onRequest hook, before the status code is known, so 4xx/5xx errors were sent with max-age and cached. Moves the logic to onSend: 2xx keep the computed value, non-2xx get no-store; preserves the existing user-set-header guard.

Closes #182

imharjot and others added 2 commits June 18, 2026 03:44
The Cache-control header was set in an onRequest hook before the
response status was known, so error responses (4xx/5xx, including
framework 404s) were served with the configured max-age. Browsers
could then replay a stale error for the duration of max-age.

Move the header to an onSend hook and only apply the configured
caching value to 2xx responses; non-2xx responses get no-store. The
existing guard that skips overriding a Cache-control header already
set upstream is preserved.

Closes fastify#182
@Tony133
Tony133 requested a review from a team July 11, 2026 10:35

@mcollina mcollina 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

@mcollina
mcollina merged commit 9e01b41 into fastify:main Jul 11, 2026
14 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.

Error responses cacheable due to unconditional Cache-Control headers

3 participants