// Veza Platform Service Worker // v1.0.9 W4 Day 16 — strategy spec (per docs/ROADMAP_V1.0_LAUNCH.md) : // - Static assets : StaleWhileRevalidate // - HLS segments : CacheFirst, max-age 7d, max 50 entries // - API GET : NetworkFirst, timeout 3s // // We intentionally stay on hand-rolled fetch handlers rather than // migrating to Workbox : the existing implementation already carries // push notifications + background sync + notificationclick, and the // strategies the roadmap asks for are 60 lines below. Workbox would // bring an additional 200+ KB of runtime + a build-step dependency // for a feature set we already cover. const CACHE_VERSION = '__BUILD_VERSION__'; const CACHE_NAME = `veza-platform-${CACHE_VERSION}`; const STATIC_CACHE_NAME = `veza-static-${CACHE_VERSION}`; const DYNAMIC_CACHE_NAME = `veza-dynamic-${CACHE_VERSION}`; // Day 16 strategy constants — tunable here, NOT inline in the helpers. const HLS_CACHE_MAX_ENTRIES = 50; const HLS_CACHE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7d const NETWORK_FIRST_TIMEOUT_MS = 3000; // 3s — beyond this, serve cached // Files to cache on install const STATIC_ASSETS = [ '/', '/dashboard', '/chat', '/library', '/profile', '/settings', '/manifest.json', '/icons/icon-192x192.png', '/icons/icon-512x512.png' ]; // API endpoints to cache with network-first strategy const API_CACHE_PATTERNS = [ /^https?:\/\/.*\/api\/v1\/user\/profile$/, /^https?:\/\/.*\/api\/v1\/library\/files$/, /^https?:\/\/.*\/api\/v1\/dashboard\/stats$/ ]; // Install event - v0.801: skip waiting, no aggressive cache clear on install self.addEventListener('install', (event) => { event.waitUntil(self.skipWaiting()); }); // Activate event - clean old version caches, claim self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((name) => !name.includes(CACHE_VERSION)) .map((name) => caches.delete(name)) ); }).then(() => self.clients.claim()) ); }); // Fetch event - handle requests with appropriate caching strategy self.addEventListener('fetch', (event) => { const { request } = event; const url = new URL(request.url); // Skip non-GET requests if (request.method !== 'GET') { return; } // Skip WebSocket connections if (request.headers.get('upgrade') === 'websocket') { return; } // v0.801: JS/CSS chunks always from network - never cache if (url.pathname.includes('/sw.js') || url.pathname.includes('/assets/') || /\.(js|css|mjs)(\?.*)?$/.test(url.pathname)) { return; } // Skip external requests (except API) if (!url.origin.includes(self.location.origin) && !isApiRequest(request.url)) { return; } event.respondWith( handleRequest(request) ); }); // Handle different types of requests with appropriate strategies async function handleRequest(request) { try { // Strategy 0: Cache First for audio files (offline playback) if (isAudioRequest(request.url)) { return await cacheAudio(request); } // Strategy 1: Cache First for static assets if (isStaticAsset(request.url)) { return await cacheFirst(request, STATIC_CACHE_NAME); } // Strategy 2: Network First for API requests if (isApiRequest(request.url)) { return await networkFirst(request, DYNAMIC_CACHE_NAME); } // Strategy 3: Stale While Revalidate for pages if (isPageRequest(request.url)) { return await staleWhileRevalidate(request, DYNAMIC_CACHE_NAME); } // Default: Network only return await fetch(request); } catch (error) { console.error('[SW] Request failed:', error); // Return offline page for navigation requests if (isPageRequest(request.url)) { return await getOfflinePage(); } // Return cached version if available const cachedResponse = await caches.match(request); if (cachedResponse) { return cachedResponse; } // Return generic offline response return new Response('Offline', { status: 503, statusText: 'Service Unavailable' }); } } // Cache First strategy async function cacheFirst(request, cacheName) { const cachedResponse = await caches.match(request); if (cachedResponse) { return cachedResponse; } const networkResponse = await fetch(request); if (networkResponse.ok) { const cache = await caches.open(cacheName); cache.put(request, networkResponse.clone()); } return networkResponse; } // Network First strategy with 3s timeout (Day 16). // Race the network request against a fixed timeout. If the network // hasn't replied within NETWORK_FIRST_TIMEOUT_MS, fall back to the // cached version IF one exists — otherwise let the request continue // (no point timing out into a hard error). async function networkFirst(request, cacheName) { const cache = await caches.open(cacheName); const cachedPromise = cache.match(request); const networkPromise = fetch(request).then((networkResponse) => { if (networkResponse.ok) { // Best-effort cache write ; clone first to avoid the // "Response body is already used" trap. cache.put(request, networkResponse.clone()).catch(() => {}); } return networkResponse; }); // If the network is slow, return the cached response after the // timeout. The network request keeps running in the background and // updates the cache for the next visit. const timed = new Promise((resolve) => { setTimeout(async () => { const cached = await cachedPromise; if (cached) { console.log('[SW] networkFirst: 3s timeout hit, serving cached'); resolve(cached); } // No cached response — let the network race continue. }, NETWORK_FIRST_TIMEOUT_MS); }); try { return await Promise.race([networkPromise, timed.then((v) => v || networkPromise)]); } catch (error) { const cached = await cachedPromise; if (cached) { console.log('[SW] networkFirst: network failed, serving cached'); return cached; } throw error; } } // Stale While Revalidate strategy // CORRECTION DURABLE: Clone la réponse IMMÉDIATEMENT pour éviter "Response body is already used" async function staleWhileRevalidate(request, cacheName) { const cachedResponse = await caches.match(request); const networkResponsePromise = fetch(request) .then((networkResponse) => { if (networkResponse.ok) { // ✅ Cloner IMMÉDIATEMENT la réponse avant toute autre opération const responseToCache = networkResponse.clone(); // Mettre en cache de manière asynchrone (sans bloquer) caches.open(cacheName).then((cache) => { cache.put(request, responseToCache).catch((err) => { console.warn('[SW] Failed to cache response:', err); }); }); } return networkResponse; }) .catch(() => null); return cachedResponse || await networkResponsePromise; } // Get offline page async function getOfflinePage() { const cache = await caches.open(STATIC_CACHE_NAME); const offlineResponse = await cache.match('/'); if (offlineResponse) { return offlineResponse; } return new Response(`
Vous êtes actuellement hors ligne. Certaines fonctionnalités peuvent être limitées.