1/* 2Taken from: https://googlechrome.github.io/samples/service-worker/custom-offline-page/index.html 3 4Copyright 2015, 2019 Google Inc. All Rights Reserved. 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 http://www.apache.org/licenses/LICENSE-2.0 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14*/ 15 16// Incrementing OFFLINE_VERSION will kick off the install event and force 17// previously cached resources to be updated from the network. 18const OFFLINE_VERSION = 11; 19const CACHE_NAME = 'www-proplus-offline-2'; 20 21const OFFLINE_URL = 'offline.html'; 22 23self.addEventListener('install', (event) => { 24 event.waitUntil((async () => { 25 const cache = await caches.open(CACHE_NAME); 26 // Setting {cache: 'reload'} in the new request will ensure that the response 27 // isn't fulfilled from the HTTP cache; i.e., it will be from the network. 28 await cache.add(new Request(OFFLINE_URL, {cache: 'reload'})); 29 })()); 30}); 31 32self.addEventListener('activate', (event) => { 33 event.waitUntil((async () => { 34 // Enable navigation preload if it's supported. 35 // See https://developers.google.com/web/updates/2017/02/navigation-preload 36 if ('navigationPreload' in self.registration) { 37 await self.registration.navigationPreload.enable(); 38 } 39 })()); 40 41 // Tell the active service worker to take control of the page immediately. 42 self.clients.claim(); 43}); 44 45self.addEventListener('fetch', (event) => { 46 // We only want to call event.respondWith() if this is a navigation request 47 // for an HTML page. 48 // 49 // Only GET navigations. A page rendered as the response to a POST (the voyo 50 // login flow does this) gets a POST history entry - going back to it makes the 51 // browser replay the navigation, and fetch() cannot re-send that body, so we 52 // would answer a perfectly normal back button with the offline page. 53 if (event.request.mode === 'navigate' && event.request.method === 'GET') { 54 event.respondWith((async () => { 55 try { 56 // First, try to use the navigation preload response if it's supported. 57 const preloadResponse = await event.preloadResponse; 58 if (preloadResponse) { 59 return preloadResponse; 60 } 61 const networkResponse = await fetch(event.request); 62 return networkResponse; 63 } catch (error) { 64 // catch is only triggered if an exception is thrown, which is likely 65 // due to a network error. 66 // If fetch() returns a valid HTTP response with a response code in 67 // the 4xx or 5xx range, the catch() will NOT be called. 68 console.log('Fetch failed; returning offline page instead.', error); 69 70 const cache = await caches.open(CACHE_NAME); 71 const cachedResponse = await cache.match(OFFLINE_URL); 72 return cachedResponse; 73 } 74 })()); 75 } 76 77 // If our if() condition is false, then this fetch handler won't intercept the 78 // request. If there are any other fetch handlers registered, they will get a 79 // chance to call event.respondWith(). If no fetch handlers call 80 // event.respondWith(), the request will be handled by the browser as if there 81 // were no service worker involvement. 82});
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.