1/* 2Copyright 2015, 2019, 2020, 2021 Google LLC. All Rights Reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12*/ 13 14// Register the Service Worker 15if ('serviceWorker' in navigator) { 16 navigator.serviceWorker 17 .register('/sw.js') 18 .then(function (reg) { 19 console.log('Service Worker Registered', reg); 20 }) 21 .catch(function (err) { 22 console.log('Error registering Service Worker', err); 23 }); 24} 25 26// Incrementing OFFLINE_VERSION will kick off the install event and force 27// previously cached resources to be updated from the network. 28// This variable is intentionally declared and unused. 29// Add a comment for your linter if you want: 30// eslint-disable-next-line no-unused-vars 31const OFFLINE_VERSION = 11; 32const CACHE_NAME = "offline"; 33// Customize this with a different URL if needed. 34const OFFLINE_URL = "error/offline.html"; 35const filesToCache = []; 36 37self.addEventListener("install", (event) => { 38 event.waitUntil( 39 (async () => { 40 const cache = await caches.open(CACHE_NAME); 41 // Setting {cache: 'reload'} in the new request ensures that the 42 // response isn't fulfilled from the HTTP cache; i.e., it will be 43 // from the network. 44 // await cache.add(new Request(OFFLINE_URL, { cache: "reload" })); 45 return cache.addAll(filesToCache); 46 })() 47 ); 48 // Force the waiting service worker to become the active service worker. 49 self.skipWaiting(); 50}); 51 52self.addEventListener("activate", (event) => { 53 event.waitUntil( 54 (async () => { 55 // Enable navigation preload if it's supported. 56 // See https://developers.google.com/web/updates/2017/02/navigation-preload 57 if ("navigationPreload" in self.registration) { 58 await self.registration.navigationPreload.enable(); 59 } 60 })() 61 ); 62 63 // Tell the active service worker to take control of the page immediately. 64 self.clients.claim(); 65}); 66 67self.addEventListener("fetch", (event) => { 68 // Only call event.respondWith() if this is a navigation request 69 // for an HTML page. 70 if (event.request.mode === "navigate") { 71 event.respondWith( 72 (async () => { 73 try { 74 // First, try to use the navigation preload response if it's 75 // supported. 76 const preloadResponse = await event.preloadResponse; 77 if (preloadResponse) { 78 return preloadResponse; 79 } 80 81 // Always try the network first. 82 const networkResponse = await fetch(event.request); 83 return networkResponse; 84 } catch (error) { 85 // catch is only triggered if an exception is thrown, which is 86 // likely due to a network error. 87 // If fetch() returns a valid HTTP response with a response code in 88 // the 4xx or 5xx range, the catch() will NOT be called. 89 console.log("Fetch failed; returning offline page instead.", error); 90 91 const cache = await caches.open(CACHE_NAME); 92 const cachedResponse = await cache.match(OFFLINE_URL); 93 return cachedResponse; 94 } 95 })() 96 ); 97 } 98 99 // If our if() condition is false, then this fetch handler won't 100 // intercept the request. If there are any other fetch handlers 101 // registered, they will get a chance to call event.respondWith(). 102 // If no fetch handlers call event.respondWith(), the request 103 // will be handled by the browser as if there were no service 104 // worker involvement. 105});
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.