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