const CACHE_NAME = 'dumpster-v1'; const STATIC_ASSETS = [ '/', '/index.html', '/manifest.json', ]; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(STATIC_ASSETS); }) ); self.skipWaiting(); }); self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((name) => name !== CACHE_NAME) .map((name) => caches.delete(name)) ); }) ); self.clients.claim(); }); self.addEventListener('fetch', (event) => { // Skip non-GET requests if (event.request.method !== 'GET') return; // Skip API requests if (event.request.url.includes('/api/')) return; // Skip WebSocket if (event.request.url.includes('/ws')) return; event.respondWith( caches.match(event.request).then((cached) => { // Return cached version, fetch new one in background const fetchPromise = fetch(event.request).then((response) => { // Only cache successful responses if (response.ok) { const clone = response.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, clone); }); } return response; }).catch(() => { // Return cached if fetch fails return cached; }); return cached || fetchPromise; }) ); }); self.addEventListener('push', (event) => { if (!event.data) return; const data = event.data.json(); const options = { body: data.body, icon: '/icons/icon-192.png', badge: '/icons/badge.png', vibrate: [100, 50, 100], data: { url: data.url || '/', }, }; event.waitUntil( self.registration.showNotification(data.title || 'Dumpster', options) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { // Focus existing window if available for (const client of clientList) { if (client.url.includes(event.notification.data.url) && 'focus' in client) { return client.focus(); } } // Otherwise open new window return clients.openWindow(event.notification.data.url); }) ); });