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