/** * Service Worker Registration Plugin * Registers the service worker globally when the app loads * This ensures push notifications work even when not on the orders page */ export default defineNuxtPlugin(async () => { if (process.client && 'serviceWorker' in navigator) { // Register service worker on app load navigator.serviceWorker.register('/sw-notifications.js', { scope: '/' }) .then(async (registration) => { console.log('[SW] Service Worker registered globally:', registration.scope) // Check for updates periodically registration.addEventListener('updatefound', () => { const newWorker = registration.installing console.log('[SW] New service worker found, installing...') newWorker?.addEventListener('statechange', () => { if (newWorker.state === 'installed' && navigator.serviceWorker.controller) { console.log('[SW] New version available, will activate on next page load') } }) }) // Ensure push subscription exists if permission was granted // This makes sure the app appears in Android notification settings try { await navigator.serviceWorker.ready if ('Notification' in window && Notification.permission === 'granted') { const subscription = await registration.pushManager.getSubscription() if (subscription) { console.log('[SW] ✅ Push subscription active - app will appear in notification settings') } else { console.log('[SW] ⚠️ Permission granted but no subscription - user needs to re-enable notifications') } } } catch (error) { console.error('[SW] Error checking push subscription:', error) } }) .catch((error) => { console.error('[SW] Service Worker registration failed:', error) }) // Handle service worker updates navigator.serviceWorker.addEventListener('controllerchange', () => { console.log('[SW] Service Worker updated, reloading page...') window.location.reload() }) } })