From efdc78d7b1a5f8865738983ca782a7f35ddc8edd Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 22 Jul 2026 15:20:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=A6=BB=E7=BA=BF=E7=BC=93=E5=AD=98=E4=B8=8E=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=AF=BC=E8=88=AA=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serviceworker.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/serviceworker.js b/serviceworker.js index e0ee6ee4..bcca3fb2 100644 --- a/serviceworker.js +++ b/serviceworker.js @@ -1,22 +1,20 @@ -var staticCacheName = "pwa-v" + new Date().getTime(); +var staticCacheName = "pwa-v2"; var filesToCache = [ - 'public/img/pwa/icon-192x192.png', - 'public/img/pwa/icon-512x512.png' + '/public/img/pwa/icon-192x192.png', + '/public/img/pwa/icon-512x512.png' ]; -/* // Cache on install self.addEventListener("install", event => { - this.skipWaiting(); event.waitUntil( caches.open(staticCacheName) .then(cache => { return cache.addAll(filesToCache); }) + .then(() => self.skipWaiting()) ) }); -*/ -/* + // Clear cache on activate self.addEventListener('activate', event => { event.waitUntil( @@ -27,19 +25,29 @@ self.addEventListener('activate', event => { .filter(cacheName => (cacheName !== staticCacheName)) .map(cacheName => caches.delete(cacheName)) ); - }) + }).then(() => self.clients.claim()) ); -});*/ +}); -// Serve from Cache +// Navigation must prefer the network so cached redirects cannot mask auth state. self.addEventListener("fetch", event => { + if (event.request.method !== 'GET') { + return; + } + + if (event.request.mode === 'navigate') { + event.respondWith( + fetch(event.request) + .catch(() => caches.match(event.request)) + ); + + return; + } + event.respondWith( caches.match(event.request) .then(response => { return response || fetch(event.request); }) - .catch(() => { - return caches.match('offline'); - }) ) });