From 9fee17af17001ed9a81f60985f0033c8b7f615cb Mon Sep 17 00:00:00 2001 From: roberts Date: Sun, 15 Mar 2026 00:54:59 -0500 Subject: [PATCH] Disable browser caching for all static assets Add no-cache middleware to set Cache-Control: no-store on CSS, JS, asset, and HTML responses so code changes appear immediately. Co-Authored-By: Claude Opus 4.6 --- backend/main.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/main.py b/backend/main.py index 7ad0439..c7b079d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -7,6 +7,8 @@ from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException, Request from fastapi.responses import FileResponse, JSONResponse from fastapi.staticfiles import StaticFiles +from starlette.middleware.base import BaseHTTPMiddleware +from starlette.responses import Response from pydantic import BaseModel from backend.auth import authenticate_user, create_token, logout @@ -93,6 +95,26 @@ app = FastAPI( ) +# --------------------------------------------------------------------------- +# No-cache middleware — prevent browsers from caching static assets +# --------------------------------------------------------------------------- + +class NoCacheStaticMiddleware(BaseHTTPMiddleware): + """Add Cache-Control: no-store to all CSS, JS, and asset responses.""" + + async def dispatch(self, request: Request, call_next): + response: Response = await call_next(request) + path = request.url.path + if path.startswith(("/css/", "/js/", "/assets/")) or path in ("/", "/desktop") or path.endswith(".html"): + response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" + response.headers["Pragma"] = "no-cache" + response.headers["Expires"] = "0" + return response + + +app.add_middleware(NoCacheStaticMiddleware) + + # --------------------------------------------------------------------------- # Auth endpoints (not behind auth — they *create* auth) # ---------------------------------------------------------------------------