Files
rpigroupplay/sw.js
T
alexspeakerandClaude Fable 5.1 3cbffd5423 vers. 3.0.2 - iPhone: il guscio riempie davvero lo schermo
Su iOS 26 (app installata) la finestra riportata al sito è più corta
dello schermo di circa 60 punti, anche se la WebView lo copre tutto:
vh, dvh e position: fixed lasciavano una fascia vuota sotto la tab bar.
Ora js/app.js misura lo schermo e imposta l'altezza del guscio
(--shell-h) solo su iPhone in modalità installata e a schermo intero.
Player, banner PWA, finestra istruzioni, avviso offline e barra di
avanzamento sono figli del guscio con position: absolute, così seguono
il guscio e non il viewport. Cache del service worker v8.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-06 15:50:12 +01:00

134 lines
4.0 KiB
JavaScript

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