const CACHE_NAME = 'chungchun-pharmacy-v1'; const STATIC_ASSETS = [ '/static/js/lottie.min.js', '/static/animations/ai-loading.json', '/static/icons/icon-192.png', '/static/icons/icon-512.png' ]; // Install: pre-cache static assets self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)) ); self.skipWaiting(); }); // Activate: clean old caches self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((keys) => Promise.all( keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)) ) ) ); self.clients.claim(); }); // Fetch: cache-first for static, network-only for dynamic self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); // Skip non-GET requests if (event.request.method !== 'GET') return; // Skip dynamic routes entirely if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/admin') || url.pathname.startsWith('/claim') || url.pathname.startsWith('/my-page') || url.pathname === '/privacy' || url.pathname === '/logout' || url.pathname === '/') { return; } // Cache-first for static assets and fonts if (url.pathname.startsWith('/static/') || url.hostname === 'fonts.googleapis.com' || url.hostname === 'fonts.gstatic.com') { event.respondWith( caches.match(event.request).then((cached) => { return cached || fetch(event.request).then((response) => { if (response.ok) { const clone = response.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)); } return response; }); }) ); } });