const CACHE_NAME = 'agency-cache-v2'; const ASSETS = [ '/', '/index.html', '/manifest.json', '/icon-192.png', '/icon-512.png' ]; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(ASSETS); }) ); }); self.addEventListener('fetch', (event) => { // Only intercept http/https requests if (!event.request.url.startsWith('http')) return; event.respondWith( caches.match(event.request).then((response) => { // Return cached response if found, otherwise fetch from network return response || fetch(event.request).catch(error => { // If network fetch fails and it's a navigation request, return index.html if (event.request.mode === 'navigate') { return caches.match('/index.html'); } // For assets, let the browser handle the failure normally console.error('Fetch failed:', event.request.url, error); throw error; }); }) ); }); self.addEventListener('push', (event) => { const data = event.data ? event.data.json() : { title: 'Notification', body: 'New update from Agency' }; const options = { body: data.body, icon: '/icon-192.png', badge: '/icon-192.png', vibrate: [100, 50, 100], data: { dateOfArrival: Date.now(), primaryKey: '1' } }; event.waitUntil( self.registration.showNotification(data.title, options) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); event.waitUntil( clients.openWindow('/') ); });