From 8e24b8f07bb70a0dd83c5d32b25ed444baf2a54a Mon Sep 17 00:00:00 2001 From: bboysoul Date: Sat, 5 Sep 2026 17:04:46 +0800 Subject: [PATCH] fix: refresh cached app shell after deployments --- backend/main.py | 5 ++++- frontend/public/sw.js | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/main.py b/backend/main.py index f97f897..634f87b 100644 --- a/backend/main.py +++ b/backend/main.py @@ -783,6 +783,9 @@ if static_dir.exists(): async def spa(path: str): root = static_dir.resolve() target = (root / path).resolve() + 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 FileResponse(target) - return FileResponse(root / "index.html") + return FileResponse(root / "index.html", headers=headers) diff --git a/frontend/public/sw.js b/frontend/public/sw.js index 7b0d4da..30549ce 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -1,5 +1,5 @@ -const CACHE = 'dodo-shell-v2' -const SHELL = ['/', '/manifest.json', '/icon-192.png', '/icon-512.png', '/apple-touch-icon.png'] +const CACHE = 'dodo-shell-v3' +const SHELL = ['/manifest.json', '/icon-192.png', '/icon-512.png', '/apple-touch-icon.png'] self.addEventListener('install', (event) => { self.skipWaiting() event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(SHELL))) @@ -11,11 +11,16 @@ self.addEventListener('fetch', (event) => { const url = new URL(event.request.url) if (url.pathname.startsWith('/api/')) return if (event.request.method !== 'GET') return + const isNavigation = event.request.mode === 'navigate' + if (isNavigation) { + event.respondWith(fetch(event.request).catch(() => caches.match('/offline.html'))) + return + } event.respondWith( caches.match(event.request).then((cached) => cached || fetch(event.request).then((response) => { const copy = response.clone() caches.open(CACHE).then((cache) => cache.put(event.request, copy)) return response - }).catch(() => caches.match('/'))), + })), ) })