1// Incrementing OFFLINE_VERSION will kick off the install event and force 2// previously cached resources to be updated from the network. 3const OFFLINE_VERSION = 1.1; 4const CACHE_NAME = 'cryptocademy-offline'; 5// Customize this with a different URL if needed. 6const OFFLINE_URL = 'offline.html'; 7 8 9 10self.addEventListener('install', (event) => { 11 event.waitUntil((async () => { 12 const cache = await caches.open(CACHE_NAME); 13 // Setting {cache: 'reload'} in the new request will ensure that the response 14 // isn't fulfilled from the HTTP cache; i.e., it will be from the network. 15 await cache.add(new Request(OFFLINE_URL, {cache: 'reload'})); 16 })()); 17}); 18 19self.addEventListener('activate', (event) => { 20 event.waitUntil((async () => { 21 // Enable navigation preload if it's supported. 22 // See https://developers.google.com/web/updates/2017/02/navigation-preload 23 if ('navigationPreload' in self.registration) { 24 await self.registration.navigationPreload.enable(); 25 } 26 })()); 27 28 // Tell the active service worker to take control of the page immediately. 29 self.clients.claim(); 30}); 31 32self.addEventListener('fetch', (event) => { 33 // We only want to call event.respondWith() if this is a navigation request 34 // for an HTML page. 35 if (event.request.mode === 'navigate') { 36 event.respondWith((async () => { 37 try { 38 // First, try to use the navigation preload response if it's supported. 39 const preloadResponse = await event.preloadResponse; 40 if (preloadResponse) { 41 return preloadResponse; 42 } 43 44 const networkResponse = await fetch(event.request); 45 return networkResponse; 46 } 47 catch (error) { 48 // catch is only triggered if an exception is thrown, which is likely 49 // due to a network error. 50 // If fetch() returns a valid HTTP response with a response code in 51 // the 4xx or 5xx range, the catch() will NOT be called. 52 console.log('Fetch failed; returning offline page instead.', error); 53 54 const cache = await caches.open(CACHE_NAME); 55 const cachedResponse = await cache.match(OFFLINE_URL); 56 57 return cachedResponse; 58 } 59 })()); 60 } 61 62 // If our if() condition is false, then this fetch handler won't intercept the 63 // request. If there are any other fetch handlers registered, they will get a 64 // chance to call event.respondWith(). If no fetch handlers call 65 // event.respondWith(), the request will be handled by the browser as if there 66 // were no service worker involvement. 67});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.