From cee4f3e0a7f7227f395db2759b9ed067c1c058cd Mon Sep 17 00:00:00 2001 From: guke Date: Mon, 29 Jun 2026 13:03:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(h5):=20no-cache=20=E6=8C=89=E5=93=8D?= =?UTF-8?q?=E5=BA=94=20content-type=20=E5=88=A4=E5=AE=9A,=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E7=9B=AE=E5=BD=95=E5=BC=8F=20URL(/h5/x/)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- app/main.py | 4 +++- tests/test_h5_hosting.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 902c1b2..6a2e044 100644 --- a/app/main.py +++ b/app/main.py @@ -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 diff --git a/tests/test_h5_hosting.py b/tests/test_h5_hosting.py index 76eeff4..2a5d09b 100644 --- a/tests/test_h5_hosting.py +++ b/tests/test_h5_hosting.py @@ -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"