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"