const CACHE_NAME = 'credittracker-v33'; const ASSETS = [ './index.html', './manifest.json', './icon.svg', './icon-maskable.svg' ]; // Install: pre-cache all app shell files self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME).then(cache => cache.addAll(ASSETS)) ); self.skipWaiting(); }); // Activate: clean up old caches self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))) ) ); self.clients.claim(); }); // Fetch: cache-first for app files, network-first for everything else self.addEventListener('fetch', event => { // Only handle same-origin GET requests if (event.request.method !== 'GET') return; event.respondWith( caches.match(event.request).then(cached => { if (cached) return cached; return fetch(event.request).then(response => { // Cache successful responses for app assets if (response.ok) { const clone = response.clone(); caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone)); } return response; }); }).catch(() => { // Offline fallback: return the app shell return caches.match('./index.html'); }) ); });