// public/service-worker.js const CACHE_NAME = 'sociowire-v2'; const PRECACHE = [ '/', '/index.html', '/icons/icon-192x192.png', '/icons/icon-512x512.png', '/icons/apple-touch-icon.png', '/icons/og-image.png' ]; self.addEventListener('install', event => { self.skipWaiting(); event.waitUntil( caches.open(CACHE_NAME).then(cache => { return cache.addAll(PRECACHE); }) ); }); self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all(keys.map(key => (key === CACHE_NAME ? null : caches.delete(key)))) ).then(() => self.clients.claim()) ); }); self.addEventListener('fetch', event => { if (event.request.mode === 'navigate') { event.respondWith( fetch(event.request) .then(response => { const copy = response.clone(); caches.open(CACHE_NAME).then(cache => cache.put('/index.html', copy)); return response; }) .catch(() => caches.match('/index.html')) ); return; } event.respondWith( caches.match(event.request).then(response => response || fetch(event.request)) ); });