const CACHE_NAME = 'agency-cache-v3'; 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) => { let data = { title: 'Notification', body: 'New update from Agency' }; try { if (event.data) { data = event.data.json(); } } catch (e) { console.error('Error parsing push data:', e); } const options = { body: data.body, icon: '/icon-192.png', badge: '/icon-192.png', vibrate: [100, 50, 100], data: { dateOfArrival: Date.now(), primaryKey: '1', taskId: data.task_id, runId: data.run_id } }; event.waitUntil( self.registration.showNotification(data.title, options) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); const taskId = event.notification.data.taskId; const runId = event.notification.data.runId; let url = '/'; if (taskId && runId) { url = `/?taskId=${taskId}&runId=${runId}`; } event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windowClients) => { // Check if there is already a window open and focus it, or open a new one for (let client of windowClients) { if ('focus' in client) { // Navigate the existing client to the new URL if it's the same app return client.navigate(url).then(c => c.focus()); } } if (clients.openWindow) { return clients.openWindow(url); } }) ); });