Files
rpigroupplay/sw.js
T
alexspeakerandClaude Fable 5.1 ffb04a96a7 Rollback alla 3.0.0 (24391c4)
Ripristinato per intero lo stato del commit 24391c4, che sull'iPhone
con l'icona installata si vedeva correttamente. Le modifiche dei commit
successivi restano nella storia per essere riprese una per volta.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-06 16:08:48 +01:00

113 lines
3.2 KiB
JavaScript

const CACHE_NAME = 'rpigroupplay-v5';
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;
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');
}
});
})
);
});