-> Conversion du temps de retard pour >=60 Min -> Amélioration du rendu EVA (AFFICHEUR ABANDONNÉ PAR SNCF, Abandon possible) -> AFFICHAGE DE TOUTES LES DONNÉES EN MÊME TEMPS
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
const CACHE_NAME = 'ferrovia-pwa-v2';
|
|
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames.filter((name) => name !== CACHE_NAME).map((name) => caches.delete(name))
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
if (event.request.method !== 'GET' || !event.request.url.startsWith(self.location.origin)) {
|
|
return;
|
|
}
|
|
|
|
// Pour la navigation (HTML / pages), stratégie Network-First systématique pour toujours charger la dernière version
|
|
const isNavigation = event.request.mode === 'navigate' || (event.request.headers.get('accept') && event.request.headers.get('accept').includes('text/html'));
|
|
|
|
if (isNavigation) {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((networkResponse) => {
|
|
if (networkResponse && networkResponse.ok) {
|
|
const clone = networkResponse.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return networkResponse;
|
|
})
|
|
.catch(() => {
|
|
return caches.match(event.request).then((cached) => cached || caches.match('/index.html') || caches.match('./index.html'));
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Pour les scripts JS et CSS, Network-First pour éviter le gel des bundles
|
|
const isCodeAsset = event.request.url.includes('/static/js/') || event.request.url.includes('/static/css/');
|
|
if (isCodeAsset) {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((networkResponse) => {
|
|
if (networkResponse && networkResponse.ok) {
|
|
const clone = networkResponse.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return networkResponse;
|
|
})
|
|
.catch(() => {
|
|
return caches.match(event.request);
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Pour les autres ressources (images, favicons, polices), Cache-First avec mise en cache réseau
|
|
event.respondWith(
|
|
caches.match(event.request).then((cachedResponse) => {
|
|
if (cachedResponse) {
|
|
return cachedResponse;
|
|
}
|
|
return fetch(event.request).then((networkResponse) => {
|
|
if (networkResponse && networkResponse.ok) {
|
|
const clone = networkResponse.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return networkResponse;
|
|
});
|
|
})
|
|
);
|
|
});
|