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// Incrementing OFFLINE_VERSION will kick off the install event and force 15// previously cached resources to be updated from the network. 16// This variable is intentionally declared and unused. 17// Add a comment for your linter if you want: 18// eslint-disable-next-line no-unused-vars 19const OFFLINE_VERSION = 1; 20const CACHE_NAME = "offline"; 21// Customize this with a different URL if needed. 22const OFFLINE_URL = "offline.html"; 23 24self.addEventListener("install", (event) => { 25 event.waitUntil( 26 (async () => { 27 const cache = await caches.open(CACHE_NAME); 28 // Setting {cache: 'reload'} in the new request will ensure that the 29 // response isn't fulfilled from the HTTP cache; i.e., it will be from 30 // the network. 31 await cache.add(new Request(OFFLINE_URL, { cache: "reload" })); 32 })() 33 ); 34 // Force the waiting service worker to become the active service worker. 35 self.skipWaiting(); 36}); 37 38self.addEventListener("activate", (event) => { 39 event.waitUntil( 40 (async () => { 41 // Enable navigation preload if it's supported. 42 // See https://developers.google.com/web/updates/2017/02/navigation-preload 43 if ("navigationPreload" in self.registration) { 44 await self.registration.navigationPreload.enable(); 45 } 46 })() 47 ); 48 49 // Tell the active service worker to take control of the page immediately. 50 self.clients.claim(); 51}); 52 53self.addEventListener("fetch", (event) => { 54 // We only want to call event.respondWith() if this is a navigation request 55 // for an HTML page. 56 if (event.request.mode === "navigate") { 57 event.respondWith( 58 (async () => { 59 try { 60 // First, try to use the navigation preload response if it's supported. 61 const preloadResponse = await event.preloadResponse; 62 if (preloadResponse) { 63 return preloadResponse; 64 } 65 66 // Always try the network first. 67 const networkResponse = await fetch(event.request); 68 return networkResponse; 69 } catch (error) { 70 // catch is only triggered if an exception is thrown, which is likely 71 // due to a network error. 72 // If fetch() returns a valid HTTP response with a response code in 73 // the 4xx or 5xx range, the catch() will NOT be called. 74 console.log("Fetch failed; returning offline page instead.", error); 75 76 const cache = await caches.open(CACHE_NAME); 77 const cachedResponse = await cache.match(OFFLINE_URL); 78 return cachedResponse; 79 } 80 })() 81 ); 82 } 83 84 // If our if() condition is false, then this fetch handler won't intercept the 85 // request. If there are any other fetch handlers registered, they will get a 86 // chance to call event.respondWith(). If no fetch handlers call 87 // event.respondWith(), the request will be handled by the browser as if there 88 // were no service worker involvement. 89});
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.