From 0dff033a7f30be91da22b3597f233e42c0dbc6eb Mon Sep 17 00:00:00 2001 From: bboysoul Date: Sat, 5 Sep 2026 17:15:16 +0800 Subject: [PATCH] fix: disable stale service worker cache --- backend/main.py | 13 +++++++++++-- frontend/src/App.vue | 2 +- frontend/src/main.ts | 7 ++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/backend/main.py b/backend/main.py index 634f87b..de6c60d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -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) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index e7d872e..d05ff0c 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -35,7 +35,7 @@ const loading = ref(false) const mobileSidebar = ref(false) const mobileDetail = ref(false) const markdownPreview = ref(false) -const showCompleted = ref(false) +const showCompleted = ref(true) const page = ref(1) const pageSize = 50 const totalTasks = ref(0) diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 9ee13b9..19f71d9 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -3,4 +3,9 @@ import App from './App.vue' import './style.css' createApp(App).mount('#app') -if ('serviceWorker' in navigator && import.meta.env.PROD) navigator.serviceWorker.register('/sw.js') +if ('serviceWorker' in navigator && import.meta.env.PROD) { + navigator.serviceWorker.getRegistrations().then((registrations) => { + registrations.forEach((registration) => registration.unregister()) + }) + caches.keys().then((keys) => keys.forEach((key) => caches.delete(key))) +}