# app/core/static_files.py """Static file serving with cache-busting-aware headers. In production, JS/CSS/etc. URLs are versioned by `static_v(...)` in `app/templates_config.py` (appends `?v=`), so the file at a given URL never changes within a deploy. We can therefore tell browsers to cache it for a year and skip revalidation. In development the version still flips per commit, but the developer often edits files without committing, so we serve `no-cache` to force conditional GETs (the browser still gets a 304 when nothing changed). """ from fastapi.staticfiles import StaticFiles from starlette.responses import FileResponse from app.core.environment import is_development _IMMUTABLE = "public, max-age=31536000, immutable" _NO_CACHE = "no-cache" class CachedStaticFiles(StaticFiles): """StaticFiles that sets Cache-Control based on environment.""" def file_response(self, *args, **kwargs) -> FileResponse: response = super().file_response(*args, **kwargs) response.headers["Cache-Control"] = ( _NO_CACHE if is_development() else _IMMUTABLE ) return response