-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
1 lines (1 loc) · 2.1 KB
/
sw.js
File metadata and controls
1 lines (1 loc) · 2.1 KB
1
const CACHE_NAME="v104",MEDIA_CACHE_NAME="media-v104",OFFLINE_URL="/offline.html",resourcesToCache=["/","/styles/main.css","/styles/print.css","/scripts/main.js","/scripts/more.js","/fonts/notoSans-Regular.woff2","/fonts/notoSerif-Regular.woff2","/sprites/util.svg","/sprites/player.svg","/medias/images/logo/logo.svg","/sprites/silos/195v.svg#a","/sprites/various/MonogrammeTULivreDeKellsTEST.svg#a",OFFLINE_URL],STATIC_DESTINATIONS=new Set(["style","script","font","image"]);function isStaticAsset(t){return!!STATIC_DESTINATIONS.has(t.destination)||/\.(css|js|woff2|svg)$/.test(t.url)}let lastNotifyTime=0;const NOTIFY_DEBOUNCE_MS=5e3;async function addResourcesToCache(t){try{const e=await caches.open("v104");await e.addAll(t)}catch(t){console.error(`Erreur cache.addAll: ${t}`)}}function putInCache(t,e){return caches.open("v104").then(a=>a.put(t,e)).catch(t=>console.error(`Erreur putInCache: ${t}`))}async function notifyServiceUnavailable(){const t=Date.now();if(!(t-lastNotifyTime<5e3)){lastNotifyTime=t;try{(await clients.matchAll()).forEach(t=>t.postMessage({action:"service-unavailable"}))}catch(t){console.error(`Erreur notification: ${t}`)}}}async function networkFirst(t){const{request:e}=t;try{const a=await fetch(e);return a?.ok&&t.waitUntil(putInCache(e,a.clone())),a}catch{await notifyServiceUnavailable();const t=await caches.open("v104");return await t.match(e)??t.match(OFFLINE_URL)}}async function cacheFirst(t){const{request:e}=t,a=await caches.open("v104"),s=await a.match(e);if(s)return s;try{const a=await fetch(e);return a?.ok&&t.waitUntil(putInCache(e,a.clone())),a}catch{return a.match(OFFLINE_URL)}}self.addEventListener("install",t=>{self.skipWaiting(),t.waitUntil(addResourcesToCache(resourcesToCache))}),self.addEventListener("activate",t=>{t.waitUntil((async()=>{try{const t=await caches.keys();await Promise.all(t.filter(t=>"v104"!==t&&"media-v104"!==t).map(t=>caches.delete(t))),await self.clients.claim()}catch(t){console.error(`Erreur activation: ${t}`)}})())}),self.addEventListener("fetch",t=>{if("GET"!==t.request.method)return;const e=isStaticAsset(t.request)?cacheFirst:networkFirst;t.respondWith(e(t))});