From dc69cf2bc7fcd78cf80b25de44b4aa94cb8de2cc Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 6 Apr 2026 10:46:49 +0000 Subject: [PATCH] fix(server): serve static files with no-cache headers to prevent stale JS/CSS --- lightsync/main.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lightsync/main.py b/lightsync/main.py index 6bf8f88..1a819e4 100644 --- a/lightsync/main.py +++ b/lightsync/main.py @@ -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