fix: use content-type instead of path for no-cache detection
Root "/" path doesn't match .html ending. Checking the response's content-type header catches index.html served via html=True and is more robust for any URL that returns HTML/JS/CSS/JSON. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
4fe0977b71
commit
21e72b438f
1 changed files with 4 additions and 3 deletions
7
main.py
7
main.py
|
|
@ -3325,9 +3325,10 @@ class NoCacheStaticFiles(StaticFiles):
|
|||
response = await super().get_response(path, scope)
|
||||
# Force revalidation for HTML/JS/CSS/JSON so code changes show up
|
||||
# immediately after git pull. Other assets (images, fonts) can cache.
|
||||
# Empty path ("") or paths ending in / resolve to index.html via html=True.
|
||||
is_html_route = path == "" or path.endswith("/") or path.endswith(".html")
|
||||
if is_html_route or any(path.endswith(ext) for ext in (".js", ".css", ".json")):
|
||||
# Check content-type header since root path "" resolves to index.html
|
||||
# via html=True and we need to catch it too.
|
||||
ct = response.headers.get("content-type", "").lower()
|
||||
if any(t in ct for t in ("text/html", "javascript", "text/css", "application/json")):
|
||||
response.headers["Cache-Control"] = "no-cache, must-revalidate"
|
||||
return response
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue