const CACHE_NAME = 'rpigroupplay-v10'; const ASSETS = [ './', './index.php', './manifest.json', './css/style.css', './css/fonts/plus-jakarta-sans-latin-wght-normal.woff2', './css/fonts/bricolage-grotesque-latin-wght-normal.woff2', './js/app.js', './js/pwa-install.js', './img/RpiGroupPlayWHITE.png', './img/logoapp.png', './img/icons/icon-192x192.png', './img/icons/icon-512x512.png' ]; // File statici dell'app: la versione nella query (?v=...) serve solo a svuotare la cache del browser, // per il service worker è lo stesso file const STATIC_RE = /\.(css|js|png|jpe?g|svg|webp|ico|json|woff2?|ttf)$/i; // Install Event self.addEventListener('install', (e) => { e.waitUntil( caches.open(CACHE_NAME) .then((cache) => { return cache.addAll(ASSETS).catch(err => { console.warn('[Service Worker] Asset caching warning:', err); }); }) .then(() => self.skipWaiting()) ); }); // Activate Event self.addEventListener('activate', (e) => { e.waitUntil( caches.keys().then((keys) => { return Promise.all( keys.map((key) => { if (key !== CACHE_NAME) { return caches.delete(key); } }) ); }).then(() => self.clients.claim()) ); }); // Fetch Event self.addEventListener('fetch', (e) => { const url = e.request.url; // Skip non-GET requests if (e.request.method !== 'GET') { return; } // Bypass streaming content, APIs, and external domains (like radio/tv streams, fonts, hls.js) if ( url.includes('stream') || url.includes('.mp3') || url.includes('.aac') || url.includes('.m3u8') || url.includes('.ts') || url.includes('icecast') || url.includes('shoutcast') || url.includes('nowplaying') || url.includes('radiocitta105') || url.includes('radiodiffusionelibera') || !url.startsWith(self.location.origin) ) { return; // Let the browser handle standard streaming/external fetch requests directly } const isStatic = STATIC_RE.test(new URL(url).pathname); const matchOptions = isStatic ? { ignoreSearch: true } : undefined; // Pagine (guscio dell'app e contenuti PHP): prima la rete, la cache solo se la rete manca. // Così dopo un aggiornamento nessuno resta con il guscio vecchio e gli script nuovi. if (!isStatic) { e.respondWith( fetch(e.request).then((response) => { if (response.status === 200) { const responseClone = response.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(e.request, responseClone)); } return response; }).catch(() => { return caches.match(e.request).then((cached) => { if (cached) return cached; if (e.request.mode === 'navigate') return caches.match('./index.php'); }); }) ); return; } // File statici (css, js, font, immagini): subito dalla cache, aggiornati in sottofondo e.respondWith( caches.match(e.request, matchOptions).then((cachedResponse) => { if (cachedResponse) { // Return cached resource immediately and fetch fresh copy in background (Stale-While-Revalidate) fetch(e.request).then((networkResponse) => { if (networkResponse.status === 200) { caches.open(CACHE_NAME).then((cache) => { cache.put(e.request, networkResponse); }); } }).catch(() => { // Silent catch network failures }); return cachedResponse; } // Fallback to network return fetch(e.request).then((response) => { // Cache standard successful requests if (response.status === 200) { const responseClone = response.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(e.request, responseClone); }); } return response; }).catch(() => { // Fallback offline: restituiamo l'app shell se l'utente sta navigando if (e.request.mode === 'navigate') { return caches.match('./index.php'); } }); }) ); });