-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.min.js
1 lines (1 loc) · 2.3 KB
/
service-worker.min.js
1
(()=>{CACHE_VERSION=1;var h=["/css/style.min.css","/js/theme.min.js","/site.webmanifest","/fixit.min.svg"],m=["/css/style.min.css","/js/theme.min.js","/site.webmanifest","/fixit.min.svg","/offline/"],E=["/css/style.min.css","/js/theme.min.js","/site.webmanifest","/fixit.min.svg","/404.html"],d="/offline/",C="/404.html",r={assets:"assets-v"+CACHE_VERSION,content:"content-v"+CACHE_VERSION,offline:"offline-v"+CACHE_VERSION,notFound:"404-v"+CACHE_VERSION},f={"/":3600,html:3600,json:86400,js:86400,css:86400},u=[t=>!t.startsWith("http://localhost")],_=["GET"];function g(t){return u.length>0?!u.filter(e=>typeof e=="function"?!e(t):!1).length:!1}function A(t){let e=t.split(".").reverse()[0].split("?")[0];return e.endsWith("/")?"/":e}function O(t){if(typeof t=="string"){let e=A(t);return typeof f[e]=="number"?f[e]:null}else return null}function S(){return Promise.all([caches.open(r.assets).then(t=>t.addAll(h)),caches.open(r.offline).then(t=>t.addAll(m)),caches.open(r.notFound).then(t=>t.addAll(E))]).then(()=>self.skipWaiting())}function F(){let t=Object.keys(r).map(e=>r[e]);return new Promise((e,s)=>{caches.keys().then(n=>n.filter(i=>!~t.indexOf(i))).then(n=>{n.length?Promise.all(n.map(i=>caches.delete(i))).then(()=>{e()}).catch(i=>{s(i)}):e()}).catch(n=>{s(n)})})}self.addEventListener("install",t=>{t.waitUntil(Promise.all([S(),self.skipWaiting()]))});self.addEventListener("activate",t=>{t.waitUntil(Promise.all([F(),self.clients.claim(),self.skipWaiting()]).catch(e=>{console.log(e),self.skipWaiting()}))});self.addEventListener("fetch",t=>{t.respondWith(caches.open(r.content).then(e=>e.match(t.request).then(s=>{if(s){let n=s.headers.entries(),i=null;for(let c of n)c[0]==="date"&&(i=new Date(c[1]));if(i){let c=parseInt((new Date().getTime()-i.getTime())/1e3),o=O(t.request.url);return o&&c>o?new Promise(l=>fetch(t.request.clone()).then(a=>{a?(e.put(t.request,a.clone()),l(a)):l(s)}).catch(()=>{l(s)})).catch(l=>(console.log(l),s)):s}else return s}else return null}).then(s=>s||fetch(t.request.clone()).then(n=>n.status<400?(~_.indexOf(t.request.method)&&!g(t.request.url)&&t.request.url.slice(0,4)==="http"&&e.put(t.request,n.clone()),n):caches.open(r.notFound).then(i=>i.match(C))).then(n=>{if(n)return n}).catch(()=>caches.open(r.offline).then(n=>n.match(d)))).catch(s=>{throw console.error(" Error in fetch handler:",s),s})))});})();