fix: do not cache non-2xx responses#192
Open
imharjot wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
Cache-controlheader was applied in anonRequesthook, before the status code is known, so 4xx/5xx errors were sent withmax-ageand cached. Moves the logic toonSend: 2xx keep the computed value, non-2xx getno-store; preserves the existing user-set-header guard.Closes #182