Conditional requests for static files (ETag / Last-Modified / 304)#298
Open
balat wants to merge 4 commits into
Open
Conditional requests for static files (ETag / Last-Modified / 304)#298balat wants to merge 4 commits into
balat wants to merge 4 commits into
Conversation
Response.respond_file now always adds a weak ETag (derived from the file's modification time and size) and a Last-Modified header, and takes optional If-None-Match / If-Modified-Since values; when they match, it answers 304 Not Modified with an empty body, keeping the validators (RFC 7232).
Pass the request's If-None-Match and If-Modified-Since headers to Response.respond_file so that unchanged static files return 304 Not Modified.
Checks that static files carry ETag and Last-Modified, that re-requesting with either validator returns 304 with an empty body, and that a non-matching ETag still serves the file.
This was referenced Jun 26, 2026
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.
What
Static files are now served with validators and honour conditional requests, so
browsers and caches can revalidate cheaply (RFC 7232) — even
python -m http.serverdid this and we did not.
modification time and size) and a Last-Modified header.
that matches gets 304 Not Modified with an empty body, keeping the
validators.
Where
Response.respond_filegained the ETag/Last-Modified generation and optional?if_none_match/?if_modified_sinceparameters; Staticmod passes therequest's headers. If-None-Match takes precedence over If-Modified-Since per
RFC 7232; the latter only returns 304 on an exact match of our Last-Modified
value, so it never answers 304 wrongly.
Tests
test/conditional-requests.tchecks the validators are present, thatre-requesting with either validator returns 304 with an empty body, and that a
non-matching ETag still serves the file.
Note
Independent of #297 (reverse proxy); both branch from master.