fix: add no-cache middleware to prevent OpenResty proxy caching HTML/JS/CSS
This commit is contained in:
@@ -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")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user