/* eslint-disable no-restricted-globals */ const CACHE_NAME = 'findagram-v1'; const urlsToCache = [ '/', '/index.html', '/manifest.json', ]; // Install service worker self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log('Opened cache'); return cache.addAll(urlsToCache); }) ); self.skipWaiting(); }); // Fetch event - network first, fallback to cache self.addEventListener('fetch', (event) => { // Skip non-GET requests if (event.request.method !== 'GET') return; // Skip chrome-extension requests if (event.request.url.startsWith('chrome-extension://')) return; event.respondWith( fetch(event.request) .then((response) => { // Clone the response const responseClone = response.clone(); // Cache successful responses if (response.status === 200) { caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, responseClone); }); } return response; }) .catch(() => { // Fallback to cache return caches.match(event.request).then((response) => { if (response) { return response; } // Return offline page for navigation requests if (event.request.mode === 'navigate') { return caches.match('/'); } return new Response('Offline', { status: 503 }); }); }) ); }); // Activate event - clean old caches self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((cacheName) => cacheName !== CACHE_NAME) .map((cacheName) => caches.delete(cacheName)) ); }) ); self.clients.claim(); }); // Push notification handling self.addEventListener('push', (event) => { if (!event.data) return; const data = event.data.json(); const options = { body: data.body || 'New notification from Find a Gram', icon: '/logo192.png', badge: '/logo192.png', vibrate: [100, 50, 100], data: { url: data.url || '/', }, }; event.waitUntil( self.registration.showNotification(data.title || 'Find a Gram', options) ); }); // Notification click handling self.addEventListener('notificationclick', (event) => { event.notification.close(); event.waitUntil( self.clients.matchAll({ type: 'window' }).then((clientList) => { // If there's already a window open, focus it for (const client of clientList) { if (client.url === event.notification.data.url && 'focus' in client) { return client.focus(); } } // Otherwise, open a new window if (self.clients.openWindow) { return self.clients.openWindow(event.notification.data.url); } }) ); });