Skip to content

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

Open
imharjot wants to merge 1 commit into
fastify:mainfrom
imharjot:fix/no-cache-error-responses
Open

fix: do not cache non-2xx responses#192
imharjot wants to merge 1 commit into
fastify:mainfrom
imharjot:fix/no-cache-error-responses

Conversation

@imharjot

Copy link
Copy Markdown

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

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

1 participant