fix(api): 前端产物加缓存头 —— index.html 不再被浏览器缓存住
`index.html` 没有内容哈希,而 StaticFiles 只发 ETag / Last-Modified、不发 Cache-Control,浏览器于是按启发式规则把它缓存住 —— 打开的还是上一次那份 index.html, 指向的还是上一次那个 js 包。现象就是「代码明明发了,界面上却看不见新功能」,只能靠 Ctrl+F5 自救,而**每次前端发布会再来一遍**。这次真的踩到了:加完功能在设置页里找不到 入口,我照着源码查了半天,最后发现是浏览器拿着旧包。 * `/`(以及任何 text/html 响应)→ `Cache-Control: no-cache`:每次都回源校验一下, 反正它只有几百字节。 * `/assets/`、`/logos/` → `public, max-age=31536000, immutable`:文件名带内容哈希, 一变就是新 URL,没有理由每次回问。 * `/api/` 一律不碰 —— 接口那套缓存由客户端和 React Query 管。 测试里断言这三条;没有构建产物时(gitignore 的)自动跳过。
This commit is contained in:
+26
@@ -162,6 +162,32 @@ app.include_router(settings_router, prefix="/api", dependencies=[Depends(require
|
|||||||
app.include_router(websocket_router, prefix="/api")
|
app.include_router(websocket_router, prefix="/api")
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def static_cache_headers(request, call_next):
|
||||||
|
"""给前端产物加缓存头。
|
||||||
|
|
||||||
|
**`index.html` 必须 no-cache。** 它没有内容哈希,浏览器会按启发式规则把它缓存住
|
||||||
|
(Starlette 的 StaticFiles 只发 ETag / Last-Modified,不发 Cache-Control),于是
|
||||||
|
打开的还是上一次那份 index.html,指向的还是上一次那个 js 包 —— 现象就是「代码明明
|
||||||
|
发了,界面上却看不见新功能」,只能靠 Ctrl+F5 自救,而每次前端发布会再来一遍。
|
||||||
|
|
||||||
|
带哈希的 `/assets/` 反过来:文件名一变就是新 URL,可以放心让它永久缓存。
|
||||||
|
只处理非 /api 的路径 —— 接口响应自己那套(缓存由客户端和 React Query 管)。
|
||||||
|
"""
|
||||||
|
response = await call_next(request)
|
||||||
|
path = request.url.path
|
||||||
|
if path.startswith("/api/"):
|
||||||
|
return response
|
||||||
|
|
||||||
|
if path.startswith("/assets/") or path.startswith("/logos/"):
|
||||||
|
response.headers.setdefault(
|
||||||
|
"Cache-Control", "public, max-age=31536000, immutable"
|
||||||
|
)
|
||||||
|
elif (response.headers.get("content-type") or "").startswith("text/html"):
|
||||||
|
response.headers.setdefault("Cache-Control", "no-cache")
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def serve_frontend():
|
async def serve_frontend():
|
||||||
"""Return frontend page"""
|
"""Return frontend page"""
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""前端产物的缓存头。
|
||||||
|
|
||||||
|
**`index.html` 必须 no-cache。** 它没有内容哈希,一旦被浏览器启发式缓存住,打开的还是
|
||||||
|
上一次那份 index.html、指向上一次那个 js 包 —— 现象就是「代码明明发了,界面上却看不见
|
||||||
|
新功能」,只能靠 Ctrl+F5 自救。这个坑真的踩过:加完功能在设置页里找不到入口,查了半天
|
||||||
|
源码,最后发现是浏览器拿着旧包。
|
||||||
|
|
||||||
|
带哈希的 `/assets/` 反过来,可以放心让它永久缓存。
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from api.main import app
|
||||||
|
|
||||||
|
# 产物是 gitignore 的:没构建过就没有静态文件可测(部署机器上一定有)。
|
||||||
|
INDEX = pathlib.Path("api/webui/index.html")
|
||||||
|
ASSETS = pathlib.Path("api/webui/assets")
|
||||||
|
|
||||||
|
needs_build = pytest.mark.skipif(
|
||||||
|
not INDEX.exists(), reason="前端产物没构建过,测不了静态文件的缓存头"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client():
|
||||||
|
with TestClient(app) as test_client:
|
||||||
|
yield test_client
|
||||||
|
|
||||||
|
|
||||||
|
@needs_build
|
||||||
|
def test_the_entry_page_is_never_cached(client):
|
||||||
|
response = client.get("/")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.headers["cache-control"] == "no-cache"
|
||||||
|
|
||||||
|
|
||||||
|
@needs_build
|
||||||
|
def test_hashed_bundles_are_cached_forever(client):
|
||||||
|
"""文件名带内容哈希,变了就是新 URL —— 没有理由每次回源。"""
|
||||||
|
bundle = next(ASSETS.glob("*.js"), None)
|
||||||
|
if bundle is None:
|
||||||
|
pytest.skip("没有 js 产物")
|
||||||
|
|
||||||
|
response = client.get(f"/assets/{bundle.name}")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.headers["cache-control"] == "public, max-age=31536000, immutable"
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_responses_are_left_alone(client):
|
||||||
|
"""接口那套缓存由客户端和 React Query 管,这里不该插手。"""
|
||||||
|
response = client.get("/api/health")
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.headers.get("cache-control") is None
|
||||||
Reference in New Issue
Block a user