orientation-member-service-worker.js (1605B)
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 "orientation-member-landscape-manual.html", 10 "orientation-member-portrait-manual.html", 11 "orientation-member-service-worker.js", 12 "resources/orientation-member-manual.js", 13 "resources/icon.png", 14 ]; 15 16 // Load all resources for this service worker. 17 const precache = async () => { 18 const cache = await caches.open(CACHE_NAME); 19 await cache.addAll(resources); 20 }; 21 22 // Get a resource from the cache. 23 const fromCache = async request => { 24 const cache = await caches.open(CACHE_NAME); 25 return await cache.match(request.url); 26 }; 27 28 // Attempt to get resources from the network first, fallback to the cache if we're 29 // offline. 30 const networkFallbackToCache = async request => { 31 try { 32 const response = await fetch(request); 33 if (response.ok) return response; 34 } catch (err) {} 35 return await fromCache(request); 36 }; 37 38 // When we have a new service worker, update the caches and swap immediately. 39 self.addEventListener("install", e => { 40 e.waitUntil(precache().then(() => self.skipWaiting())); 41 }); 42 43 // Claim existing clients. 44 self.addEventListener("activate", e => { 45 e.waitUntil(self.clients.claim()); 46 }); 47 48 // When a resource need to be fetched, check whether it is 49 // contained in the cache and return the cached version, otherwise 50 // get it from the network. 51 self.addEventListener("fetch", e => { 52 e.respondWith(networkFallbackToCache(e.request)); 53 });