const CACHE = 'workout-five-v1' const ASSETS = ['/', '/manifest.json', '/icon-192.png', '/icon-512.png', '/apple-touch-icon.png'] self.addEventListener('install', (e) => { e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)).then(() => self.skipWaiting())) }) self.addEventListener('activate', (e) => { e.waitUntil( caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))).then(() => self.clients.claim()) ) }) self.addEventListener('fetch', (e) => { const url = new URL(e.request.url) if (e.request.method !== 'GET' || url.pathname.startsWith('/api/')) { return // API 走网络 } e.respondWith( caches.match(e.request).then((hit) => hit || fetch(e.request).then((resp) => { const clone = resp.clone() caches.open(CACHE).then((c) => c.put(e.request, clone)) return resp })) ) })