fix: add no-cache middleware to prevent OpenResty proxy caching HTML/JS/CSS

This commit is contained in:
2026-06-09 16:03:34 +00:00
parent 6f58b97590
commit 77b40848b8

View File

@@ -584,6 +584,18 @@ def health():
# ─── Mount static frontend (at /) ────────────────────── # ─── Mount static frontend (at /) ──────────────────────
# Static files mounted after API routes to avoid conflicts # Static files mounted after API routes to avoid conflicts
static_dir = ROOT / "static" static_dir = ROOT / "static"
@app.middleware("http")
async def add_no_cache_headers(request: Request, call_next):
"""Add no-cache headers to HTML/JS/CSS to prevent OpenResty proxy caching."""
response = await call_next(request)
path = request.url.path
if path == "/" or any(path.endswith(ext) for ext in (".html", ".js", ".css")):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
if static_dir.exists() and any(static_dir.iterdir()): if static_dir.exists() and any(static_dir.iterdir()):
app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static") app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static")