// Service Worker registration export function registerServiceWorker() { if (typeof window !== 'undefined' && 'serviceWorker' in navigator) { // Register immediately, don't wait for load navigator.serviceWorker .register('/sw.js', { scope: '/', }) .then((registration) => { console.log('Service Worker registered:', registration.scope) // Check for updates registration.addEventListener('updatefound', () => { const newWorker = registration.installing if (newWorker) { newWorker.addEventListener('statechange', () => { if (newWorker.state === 'installed' && navigator.serviceWorker.controller) { // New service worker available console.log('New service worker available') } }) } }) }) .catch((error) => { console.error('Service Worker registration failed:', error) }) // Listen for controller changes (when a new service worker takes over) navigator.serviceWorker.addEventListener('controllerchange', () => { console.log('Service Worker controller changed') // Optionally reload the page to use the new service worker // window.location.reload() }) } }