shortcuts-member-service-worker.js (2008B)
1 // Some user agents only offer app installation if there is a SW and it handles 2 // offline requests. 3 4 const cacheVersion = "1.2"; 5 const CACHE_NAME = `cache-v${cacheVersion}`; 6 7 // The resources cached by this service worker. 8 const resources = [ 9 "shortcuts-member-cors-fail-manual.sub.html", 10 "shortcuts-member-cors-manual.sub.html", 11 "shortcuts-member-csp-fail-manual.sub.html", 12 "shortcuts-member-csp-manual.sub.html", 13 "shortcuts-member-manual.html", 14 "shortcuts-member-skip-for-empty-name-manual.html", 15 "shortcuts-member-skip-for-invalid-url-manual.html", 16 "shortcuts-member-skip-for-out-of-scope-url-manual.html", 17 "shortcuts-member-skip-for-undefined-name-manual.html", 18 "shortcuts-member-skip-for-undefined-url-manual.html", 19 "shortcuts-member-service-worker.js", 20 "resources/shortcuts-member-manual.js", 21 "resources/pass.png", 22 ]; 23 24 // Load all resources for this service worker. 25 const precache = async () => { 26 const cache = await caches.open(CACHE_NAME); 27 await cache.addAll(resources); 28 }; 29 30 // Get a resource from the cache. 31 const fromCache = async request => { 32 const cache = await caches.open(CACHE_NAME); 33 return await cache.match(request.url); 34 }; 35 36 // Attempt to get resources from the network first, fallback to the cache if we're 37 // offline. 38 const networkFallbackToCache = async request => { 39 try { 40 const response = await fetch(request); 41 if (response.ok) return response; 42 } catch (err) {} 43 return await fromCache(request); 44 }; 45 46 // When we have a new service worker, update the caches and swap immediately. 47 self.addEventListener("install", e => { 48 e.waitUntil(precache().then(() => self.skipWaiting())); 49 }); 50 51 // Claim existing clients. 52 self.addEventListener("activate", e => { 53 e.waitUntil(self.clients.claim()); 54 }); 55 56 // When a resource need to be fetched, check whether it is 57 // contained in the cache and return the cached version, otherwise 58 // get it from the network. 59 self.addEventListener("fetch", e => { 60 e.respondWith(networkFallbackToCache(e.request)); 61 });