aboutsummaryrefslogtreecommitdiff
path: root/static/sw.min.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/sw.min.js')
-rw-r--r--static/sw.min.js2
1 files changed, 1 insertions, 1 deletions
diff --git a/static/sw.min.js b/static/sw.min.js
index f8700c9..89738c1 100644
--- a/static/sw.min.js
+++ b/static/sw.min.js
@@ -1 +1 @@
-class Pwa{constructor(t){this.scope=t;var e=new URL(location).searchParams.get("v");this.CACHE_VERSION=e,this.BASE_CACHE_FILES=["/js/theme.min.js","/js/theme_light.min.js","/abridge.css","/js/abridge.min.js","/","/404.html","/offline/","/manifest.json"],this.host=t.location.protocol+"//"+t.location.host,console.info("Host: "+this.host),this.OFFLINE_PAGE="/offline/",this.NOT_FOUND_PAGE="/404.html",this.CACHE_NAME="content-v"+this.CACHE_VERSION,this.NORM_TTL=86400,this.LONG_TTL=604800,this.TTL_NORM=["sw.min.js","sw_load.min.js"],this.TTL_LONG=["js","css"],this.TTL_EXEMPT=["jpg","jpeg","png","gif","webp","avif","ico","svg","otf","eot","ttf","woff","woff2","xml","xsl","txt","mp4","webm","mp3","ogg"]}canCache(t){return!t.startsWith("http://localhost")&&t.toString().startsWith(this.host)}getFileExtension(t){t=t.split(".").reverse()[0].split("?")[0];return t.endsWith("/")?"/":t}getFileName(t){t=t.substring(t.lastIndexOf("/")+1).split("?")[0];return t.endsWith("/")?"/":t}getTTL(t){var e;return"string"==typeof t?(e=this.getFileExtension(t),t=this.getFileName(t),-1<this.TTL_NORM.indexOf(t)?this.NORM_TTL:-1<this.TTL_LONG.indexOf(e)?this.LONG_TTL:-1<this.TTL_EXEMPT.indexOf(e)?null:this.NORM_TTL):null}async installServiceWorker(){try{return await caches.open(this.CACHE_NAME).then(t=>t.addAll(this.BASE_CACHE_FILES),t=>console.error("Error with "+this.CACHE_NAME,t)),this.scope.skipWaiting()}catch(t){return console.error("Error with installation: ",t)}}cleanupLegacyCache(){const i=[this.CACHE_NAME];return new Promise((e,s)=>{caches.keys().then(t=>t.filter(t=>!~i.indexOf(t))).then(t=>{t.length?Promise.all(t.map(t=>caches.delete(t))).then(()=>e()).catch(t=>{console.error("Error in legacy cleanup: ",t),s(t)}):e()}).catch(t=>{console.error("Error in legacy cleanup: ",t),s(t)})})}async preCacheUrl(e){const s=await caches.open(this.CACHE_NAME);return await s.match(e)?null:fetch(e).then(t=>s.put(e,t.clone()))}register(){this.scope.addEventListener("install",t=>{t.waitUntil(Promise.all([this.installServiceWorker(),this.scope.skipWaiting()])),console.info("SW Installed")}),this.scope.addEventListener("activate",e=>{e.waitUntil(Promise.all([this.cleanupLegacyCache(),this.scope.clients.claim(),this.scope.skipWaiting()]).catch(t=>{console.error("Activation error: ",t),e.skipWaiting()}))}),this.scope.addEventListener("fetch",a=>{a.respondWith(caches.open(this.CACHE_NAME).then(async e=>{if(!this.canCache(a.request.url))return fetch(a.request);const s=await e.match(a.request);if(s){var i;let t=null;for(i of s.headers.entries())if("date"===i[0]){t=new Date(i[1]);break}if(!t)return s;var n=parseInt((((new Date).getTime()-t.getTime())/1e3).toString()),r=this.getTTL(a.request.url);if(null===r||r&&n<r)return s}return fetch(a.request.clone()).then(t=>t.status<400?(this.canCache(a.request.url)&&e.put(a.request,t.clone()),t):e.match(this.NOT_FOUND_PAGE)).catch(t=>s||(console.error(`Error fetching ${a.request.url} resulted in offline`,t),e.match(this.OFFLINE_PAGE)))}))})}}const pwa=new Pwa(self);pwa.register(); \ No newline at end of file
+class Pwa{constructor(t){this.scope=t;var e=new URL(location).searchParams.get("v");this.CACHE_VERSION=e,this.BASE_CACHE_FILES=["/js/theme.min.js","/js/theme_light.min.js","/abridge.css","/js/abridge.min.js","/","/404.html","/offline/","/manifest.json"],this.host=t.location.protocol+"//"+t.location.host,this.OFFLINE_PAGE="/offline/",this.NOT_FOUND_PAGE="/404.html",this.CACHE_NAME="content-v"+this.CACHE_VERSION,this.NORM_TTL=86400,this.LONG_TTL=604800,this.TTL_NORM=["sw.min.js","sw_load.min.js"],this.TTL_LONG=["js","css"],this.TTL_EXEMPT=["jpg","jpeg","png","gif","webp","avif","ico","svg","otf","eot","ttf","woff","woff2","xml","xsl","txt","mp4","webm","mp3","ogg"]}canCache(t){return!t.startsWith("http://localhost")&&t.toString().startsWith(this.host)}getFileExtension(t){t=t.split(".").reverse()[0].split("?")[0];return t.endsWith("/")?"/":t}getFileName(t){t=t.substring(t.lastIndexOf("/")+1).split("?")[0];return t.endsWith("/")?"/":t}getTTL(t){var e;return"string"==typeof t?(e=this.getFileExtension(t),t=this.getFileName(t),-1<this.TTL_NORM.indexOf(t)?this.NORM_TTL:-1<this.TTL_LONG.indexOf(e)?this.LONG_TTL:-1<this.TTL_EXEMPT.indexOf(e)?null:this.NORM_TTL):null}async installServiceWorker(){try{return await caches.open(this.CACHE_NAME).then(t=>t.addAll(this.BASE_CACHE_FILES),t=>console.error("Error with "+this.CACHE_NAME,t)),this.scope.skipWaiting()}catch(t){return console.error("Error with installation: ",t)}}cleanupLegacyCache(){const i=[this.CACHE_NAME];return new Promise((e,s)=>{caches.keys().then(t=>t.filter(t=>!~i.indexOf(t))).then(t=>{t.length?Promise.all(t.map(t=>caches.delete(t))).then(()=>e()).catch(t=>{console.error("Error in legacy cleanup: ",t),s(t)}):e()}).catch(t=>{console.error("Error in legacy cleanup: ",t),s(t)})})}async preCacheUrl(e){const s=await caches.open(this.CACHE_NAME);return await s.match(e)?null:fetch(e).then(t=>s.put(e,t.clone()))}register(){this.scope.addEventListener("install",t=>{t.waitUntil(Promise.all([this.installServiceWorker(),this.scope.skipWaiting()]))}),this.scope.addEventListener("activate",e=>{e.waitUntil(Promise.all([this.cleanupLegacyCache(),this.scope.clients.claim(),this.scope.skipWaiting()]).catch(t=>{console.error("Activation error: ",t),e.skipWaiting()}))}),this.scope.addEventListener("fetch",a=>{a.respondWith(caches.open(this.CACHE_NAME).then(async e=>{if(!this.canCache(a.request.url))return fetch(a.request);const s=await e.match(a.request);if(s){var i;let t=null;for(i of s.headers.entries())if("date"===i[0]){t=new Date(i[1]);break}if(!t)return s;var r=parseInt((((new Date).getTime()-t.getTime())/1e3).toString()),n=this.getTTL(a.request.url);if(null===n||n&&r<n)return s}return fetch(a.request.clone()).then(t=>t.status<400?(this.canCache(a.request.url)&&e.put(a.request,t.clone()),t):e.match(this.NOT_FOUND_PAGE)).catch(t=>s||(console.error(`Error fetching ${a.request.url} resulted in offline`,t),e.match(this.OFFLINE_PAGE)))}))})}}const pwa=new Pwa(self);pwa.register(); \ No newline at end of file