web.py — Cache-Control : index.html no-cache (fin des vieilles versions servies après déploiement), /assets immuables 1 an
1 changed file +15 −1
modified
louka/web.py
+15 −1
@@ -321,9 +321,23 @@ def trigger_sync(background: BackgroundTasks, source: str | None = None): | ||
| 321 | 321 | if FRONTEND_DIST.exists(): |
| 322 | 322 | app.mount("/assets", StaticFiles(directory=FRONTEND_DIST / "assets"), name="assets") |
| 323 | 323 | |
| 324 | + @app.middleware("http") | |
| 325 | + async def _cache_headers(request, call_next): | |
| 326 | + """Politique de cache : les bundles hachés (/assets/…) sont immuables, | |
| 327 | + mais index.html doit TOUJOURS être revalidé — sinon les navigateurs | |
| 328 | + gardent une vieille version de l'app après un déploiement.""" | |
| 329 | + resp = await call_next(request) | |
| 330 | + path = request.url.path | |
| 331 | + if path.startswith("/assets/"): | |
| 332 | + resp.headers["Cache-Control"] = "public, max-age=31536000, immutable" | |
| 333 | + elif "text/html" in (resp.headers.get("content-type") or ""): | |
| 334 | + resp.headers["Cache-Control"] = "no-cache" | |
| 335 | + return resp | |
| 336 | + | |
| 324 | 337 | @app.get("/{full_path:path}") |
| 325 | 338 | def spa(full_path: str): |
| 326 | 339 | target = FRONTEND_DIST / full_path |
| 327 | 340 | if full_path and target.is_file(): |
| 328 | 341 | return FileResponse(target) |
| 329 | − return FileResponse(FRONTEND_DIST / "index.html") | |
| 342 | + return FileResponse(FRONTEND_DIST / "index.html", | |
| 343 | + headers={"Cache-Control": "no-cache"}) | |
| 330 | 344 | |