-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
6 lines (5 loc) · 1.44 KB
/
sw.js
1
2
3
4
5
6
var staticCaches=["mobirise-cache-v1"];function inArray(a,c){return 0<a.filter(function(b){return b===c}).length?!0:!1}self.addEventListener("install",function(a){console.log("SW: Installed and updated");self.skipWaiting()});
self.addEventListener("activate",function(a){console.log("SW: Activate");a.waitUntil(caches.keys().then(function(a){return Promise.all(a.map(function(b){if(!inArray(staticCaches,b))return caches.delete(b)}))}).then(function(){console.log("SW: First time caching ...");return caches.open(staticCaches).then(function(a){return fetch("/sw-resources.json").then(function(a){return a.json()}).then(function(b){b=b.reduce(function(a,b){/(?:json|html|mobirise)/i.test(b.split(".").pop())||a.push(b);return a},
["/","manifest.json"]);return Promise.all(b.map(function(b){return fetch(b,{mode:"no-cors"}).then(function(d){return a.put(b,d)})}))})}).catch(function(a){console.error(a)})}))});
self.addEventListener("fetch",function(a){"http"===a.request.url.slice(0,4)&&a.respondWith(fetch(a.request).then(function(c){if(404==c.status)return new Response("Page not found!");var b=c.clone();caches.open(staticCaches).then(function(c){0===a.request.url.indexOf("http")&&c.matchAll(a.request,{ignoreSearch:!0}).then(function(a){return Promise.all(a.map(function(a){return c.delete(a)}))}).then(function(){c.put(a.request,b)})});return c}).catch(function(c){console.log("Offline mode.");return caches.match(a.request).then(function(a){return a?
a:!1})}))});