feat: cache-header für pwa-dateien
- index.html, sw.js und Manifest mit no-cache, damit Updates ankommen - gehashte Assets mit max-age=31536000, immutable; Icons einen Tag - Manifest mit Content-Type application/manifest+json Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ad6d248cf
commit
9cf163c295
2 files changed
+59
-2
No files matched your search
+20
-2
@@ -74,6 +74,20 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
|
||||
task.cancel()
|
||||
|
||||
|
||||
# Hashed build output never changes; everything else must be revalidated so updates arrive
|
||||
_IMMUTABLE = "public, max-age=31536000, immutable"
|
||||
_REVALIDATE = "no-cache"
|
||||
_MEDIA_TYPES = {".webmanifest": "application/manifest+json"}
|
||||
|
||||
|
||||
def _cache_control(relative: str) -> str:
|
||||
if relative.startswith("assets/") or relative.startswith("workbox-"):
|
||||
return _IMMUTABLE
|
||||
if relative.startswith("icons/"):
|
||||
return "public, max-age=86400"
|
||||
return _REVALIDATE # index.html, sw.js, manifest.webmanifest
|
||||
|
||||
|
||||
def _register_frontend(app: FastAPI, static_dir: Path) -> None:
|
||||
"""Serve the built Vue SPA; unknown non-API paths fall back to index.html."""
|
||||
root = static_dir.resolve()
|
||||
@@ -85,12 +99,16 @@ def _register_frontend(app: FastAPI, static_dir: Path) -> None:
|
||||
raise HTTPException(status_code=404)
|
||||
candidate = (root / path).resolve()
|
||||
if path and candidate.is_relative_to(root) and candidate.is_file():
|
||||
return FileResponse(candidate)
|
||||
return FileResponse(
|
||||
candidate,
|
||||
media_type=_MEDIA_TYPES.get(candidate.suffix),
|
||||
headers={"Cache-Control": _cache_control(path)},
|
||||
)
|
||||
# Paths that look like files (/.env, /wp-login.php, /.git/config) are not app routes
|
||||
segments = path.split("/")
|
||||
if "." in segments[-1] or any(seg.startswith(".") for seg in segments):
|
||||
raise HTTPException(status_code=404)
|
||||
return FileResponse(index_file, headers={"Cache-Control": "no-cache"})
|
||||
return FileResponse(index_file, headers={"Cache-Control": _REVALIDATE})
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
|
||||
Reference in new issue
Block a user