fix(h5): no-cache 按响应 content-type 判定,覆盖目录式 URL(/h5/x/)

html=True 时 /h5/x/ 经 Starlette 回落 index.html,但 path 仍为 x/ 不带 .html,旧判定漏设 no-cache。改用响应 content-type=text/html 判定,并加目录式 URL 回归测试。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
guke
2026-06-29 13:03:36 +08:00
parent 4fb8f6447c
commit cee4f3e0a7
2 changed files with 10 additions and 1 deletions
+3 -1
View File
@@ -148,7 +148,9 @@ app.mount(
class _NoCacheHTMLStaticFiles(StaticFiles):
async def get_response(self, path: str, scope):
resp = await super().get_response(path, scope)
if path.endswith(".html"):
# 按响应 content-type 判定,而非请求 path:html=True 时目录式 URL(/h5/x/)
# 经 Starlette 内部回落到 index.html,此刻 path 仍是 "x/"(不带 .html)。
if resp.headers.get("content-type", "").startswith("text/html"):
resp.headers["Cache-Control"] = "no-cache"
return resp
+7
View File
@@ -12,3 +12,10 @@ def test_h5_html_no_cache(client) -> None:
resp = client.get("/h5/mine/index.html")
assert resp.status_code == 200
assert resp.headers.get("cache-control") == "no-cache"
def test_h5_html_no_cache_via_dir_url(client) -> None:
# 目录式 URL(WebView 规范入口)经 html=True 回落 index.html,也必须带 no-cache
resp = client.get("/h5/mine/")
assert resp.status_code == 200
assert resp.headers.get("cache-control") == "no-cache"