This commit is contained in:
team3
2026-06-24 07:19:01 +02:00
parent 9e888c216a
commit 12125d0f2d
2 changed files with 60 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.middleware.gzip import GZipMiddleware
from logsetup import setup_logging
@@ -22,9 +23,24 @@ async def lifespan(app: FastAPI):
await close_db()
class CachedStatic(StaticFiles):
"""StaticFiles mit Cache-Control: gehashte Assets dauerhaft (immutable),
index.html nie cachen (verweist immer auf die aktuellen Asset-Hashes)."""
async def get_response(self, path, scope):
resp = await super().get_response(path, scope)
if path.startswith("assets/"):
resp.headers["Cache-Control"] = "public, max-age=31536000, immutable"
else:
resp.headers["Cache-Control"] = "no-cache"
return resp
app = FastAPI(title="Creator", lifespan=lifespan)
# gzip für JS/CSS-Bundle + große JSON-Antworten (~1,39 MB JS → ~400 KB).
app.add_middleware(GZipMiddleware, minimum_size=500)
app.include_router(router)
if FRONTEND_DIST.exists():
app.mount("/", StaticFiles(directory=FRONTEND_DIST, html=True), name="frontend")
app.mount("/", CachedStatic(directory=FRONTEND_DIST, html=True), name="frontend")