const CACHE_NAME = 'whisky-vault-v4'; // Increment version for "Ironclad" strategy // CONFIG: Core pages and assets to pre-cache immediately on install const CORE_PAGES = [ '/', // Dashboard / Home '/tasting/new', // Critical: Add Tasting Screen '/scan', // Critical: Scan Screen ]; const STATIC_ASSETS = [ '/manifest.json', '/icon-192.png', '/icon-512.png', '/favicon.ico', ]; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log('⚡ PWA: Pre-caching core pages and assets...'); // Combine items to cache return cache.addAll([...CORE_PAGES, ...STATIC_ASSETS]); }) ); self.skipWaiting(); }); self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cacheName) => { if (cacheName !== CACHE_NAME) { return caches.delete(cacheName); } }) ); }) ); self.clients.claim(); }); // Helper for Network-First with Timeout function fetchWithTimeout(request, timeoutMs = 3000) { return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { reject(new Error('Network timeout')); }, timeoutMs); fetch(request).then( (response) => { clearTimeout(timeoutId); resolve(response); }, (err) => { clearTimeout(timeoutId); reject(err); } ); }); } self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); // CRITICAL: Always bypass cache for auth, api, and supabase requests const isAuthRequest = url.pathname.includes('/auth/') || url.pathname.includes('/v1/auth/') || url.pathname.includes('/v1/token'); const isApiRequest = url.pathname.includes('/api/'); const isSupabaseRequest = url.hostname.includes('supabase.co'); if (isAuthRequest || isApiRequest || isSupabaseRequest) { return; } // 1. NEXT.JS DATA (RSC): Stale-While-Revalidate with empty JSON fallback if (url.pathname.startsWith('/_next/data/')) { event.respondWith( fetchWithTimeout(event.request, 2000) .catch(async () => { const cachedResponse = await caches.match(event.request); if (cachedResponse) return cachedResponse; // Fallback to empty JSON to prevent "Application Error" screens return new Response(JSON.stringify({}), { headers: { 'Content-Type': 'application/json' } }); }) ); return; } // 2. ASSETS & APP SHELL: Stale-While-Revalidate const isAsset = event.request.destination === 'style' || event.request.destination === 'script' || event.request.destination === 'worker' || event.request.destination === 'font' || event.request.destination === 'image' || url.pathname.startsWith('/_next/static'); if (isAsset) { event.respondWith( caches.match(event.request).then((cachedResponse) => { const fetchPromise = fetch(event.request).then((networkResponse) => { if (networkResponse && networkResponse.status === 200) { const responseClone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, responseClone); }); } return networkResponse; }); return cachedResponse || fetchPromise; }) ); return; } // 3. NAVIGATION: Ironclad Navigation Fallback if (event.request.mode === 'navigate') { event.respondWith( fetchWithTimeout(event.request, 3000) .catch(async () => { console.log('[SW] Navigation network failure, attempting cache fallback'); const cachedResponse = await caches.match(event.request); if (cachedResponse) return cachedResponse; // CRITICAL FALLBACK: Load the Root App Shell ('/') // This allows Next.js to bootstrap and handle the routing client-side console.warn('⚠️ PWA: Route not in cache, fallback to Root App Shell'); return caches.match('/'); }) ); return; } });