fix(server): serve static files with no-cache headers to prevent stale JS/CSS
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from lightsync.devices.registry import DeviceRegistry
|
||||
@@ -33,9 +34,17 @@ def create_app() -> FastAPI:
|
||||
app.include_router(devices.router, prefix="/api/devices")
|
||||
app.include_router(ws.router)
|
||||
|
||||
# Static file serving — MUST be last (catches all unmatched paths)
|
||||
# Static file serving — no-cache headers to prevent stale JS/CSS
|
||||
frontend_dir = Path(__file__).parent / "frontend"
|
||||
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="frontend")
|
||||
|
||||
@app.get("/{filename:path}")
|
||||
async def static_files(request: Request, filename: str):
|
||||
path = frontend_dir / (filename or "index.html")
|
||||
if not path.exists() or not path.is_file():
|
||||
path = frontend_dir / "index.html"
|
||||
response = FileResponse(path)
|
||||
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
||||
return response
|
||||
|
||||
return app
|
||||
|
||||
|
||||
Reference in New Issue
Block a user