fix: disable stale service worker cache
ci / docker (push) Successful in 3m41s

This commit is contained in:
2026-09-05 17:15:16 +08:00
parent 8e24b8f07b
commit 0dff033a7f
3 changed files with 18 additions and 4 deletions
+11 -2
View File
@@ -9,7 +9,7 @@ from uuid import UUID
from fastapi import Depends, FastAPI, HTTPException, Query, Request, Response
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.responses import FileResponse, JSONResponse
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from sqlalchemy import delete, exists, func, or_, select, update
from sqlalchemy.exc import IntegrityError
@@ -786,6 +786,15 @@ if static_dir.exists():
headers = {"Cache-Control": "no-cache, no-store, must-revalidate, max-age=0"}
if target.is_file() and target.is_relative_to(root):
if target.name == "sw.js":
return FileResponse(target, headers=headers)
return HTMLResponse(
"self.addEventListener('install',()=>self.skipWaiting());"
"self.addEventListener('activate',e=>e.waitUntil("
"caches.keys().then(k=>Promise.all(k.map(x=>caches.delete(x))))"
".then(()=>self.registration.unregister())"
".then(()=>self.clients.matchAll({type:'window'}))"
".then(cs=>cs.forEach(c=>c.navigate(c.url)))));",
media_type="application/javascript",
headers=headers,
)
return FileResponse(target)
return FileResponse(root / "index.html", headers=headers)