-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.min.js
1 lines (1 loc) · 3.15 KB
/
sw.min.js
1
const CACHE_VERSION=new URL(location).searchParams.get("version"),BASE_CACHE_FILES=["/css/home.min.css","/css/page.min.css","/css/about.min.css","/js/theme.min.js","/site.webmanifest","/offline/index.html","/404.html"],OFFLINE_CACHE_FILES=["/css/home.min.css","/js/theme.min.js","/offline/index.html"],NOT_FOUND_CACHE_FILES=["/css/home.min.css","/css/page.min.css","/js/theme.min.js","/404.html"],OFFLINE_PAGE="/offline/index.html",NOT_FOUND_PAGE="/404.html",CACHE_VERSIONS={assets:"assets-v"+CACHE_VERSION,content:"content-v"+CACHE_VERSION,offline:"offline-v"+CACHE_VERSION,notFound:"404-v"+CACHE_VERSION},MAX_TTL={"/":3600,html:3600,json:86400,js:86400,css:86400},CACHE_BLACKLIST=[a=>!a.startsWith("http://localhost")],SUPPORTED_METHODS=["GET"];function isBlacklisted(a){return CACHE_BLACKLIST.length>0&&!CACHE_BLACKLIST.filter(b=>typeof b=="function"&&!b(a)).length}function getFileExtension(b){let a=b.split(".").reverse()[0].split("?")[0];return a.endsWith("/")?"/":a}function getTTL(a){if(typeof a=="string"){let b=getFileExtension(a);return typeof MAX_TTL[b]=="number"?MAX_TTL[b]:null}else return null}function installServiceWorker(){return Promise.all([caches.open(CACHE_VERSIONS.assets).then(a=>a.addAll(BASE_CACHE_FILES)),caches.open(CACHE_VERSIONS.offline).then(a=>a.addAll(OFFLINE_CACHE_FILES)),caches.open(CACHE_VERSIONS.notFound).then(a=>a.addAll(NOT_FOUND_CACHE_FILES))]).then(()=>self.skipWaiting())}function cleanupLegacyCache(){let a=Object.keys(CACHE_VERSIONS).map(a=>CACHE_VERSIONS[a]);return new Promise((b,c)=>{caches.keys().then(b=>legacyKeys=b.filter(b=>!~a.indexOf(b))).then(a=>{a.length?Promise.all(a.map(a=>caches.delete(a))).then(()=>{b()}).catch(a=>{c(a)}):b()}).catch(()=>{c()})})}function precacheUrl(a){isBlacklisted(a)||caches.open(CACHE_VERSIONS.content).then(b=>{b.match(a).then(b=>b?null:fetch(a)).then(c=>c?b.put(a,c.clone()):null)})}self.addEventListener("install",a=>{a.waitUntil(Promise.all([installServiceWorker(),self.skipWaiting()]))}),self.addEventListener("activate",a=>{a.waitUntil(Promise.all([cleanupLegacyCache(),self.clients.claim(),self.skipWaiting()]).catch(b=>{a.skipWaiting()}))}),self.addEventListener("fetch",a=>{a.respondWith(caches.open(CACHE_VERSIONS.content).then(b=>b.match(a.request).then(c=>{if(c){let e=c.headers.entries(),d=null;for(let a of e)a[0]==="date"&&(d=new Date(a[1]));if(d){let f=parseInt(((new Date).getTime()-d.getTime())/1e3),e=getTTL(a.request.url);return e&&f>e?new Promise(d=>fetch(a.request.clone()).then(e=>{e?(b.put(a.request,e.clone()),d(e)):d(c)}).catch(()=>{d(c)})).catch(a=>c):c}else return c}else return null}).then(c=>c||fetch(a.request.clone()).then(c=>c.status<400?(~SUPPORTED_METHODS.indexOf(a.request.method)&&!isBlacklisted(a.request.url)&&b.put(a.request,c.clone()),c):caches.open(CACHE_VERSIONS.notFound).then(a=>a.match(NOT_FOUND_PAGE))).then(a=>{if(a)return a}).catch(()=>caches.open(CACHE_VERSIONS.offline).then(a=>a.match(OFFLINE_PAGE)))).catch(a=>{throw console.error(" Error in fetch handler:",a),a})))}),self.addEventListener("message",a=>{if(typeof a.data=="object"&&typeof a.data.action=="string")switch(a.data.action){case"cache":precacheUrl(a.data.url);break;default:console.log("Unknown action: "+a.data.action);break}})