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. 16const OFFLINE_VERSION = 2; // ÐбÑлÑÑено веÑÑÑÑ 17const CACHE_NAME = `offline-v${OFFLINE_VERSION}`; 18// Customize this with a different URL if needed. 19const OFFLINE_URL = "offline.html"; 20 21self.addEventListener("install", (event) => { 22 console.log(`Service Worker ${OFFLINE_VERSION} installing...`); 23 24 event.waitUntil( 25 (async () => { 26 try { 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 console.log("Offline page cached successfully"); 33 } catch (error) { 34 console.error("Failed to cache offline page:", error); 35 } 36 })() 37 ); 38 39 // Force the waiting service worker to become the active service worker. 40 self.skipWaiting(); 41}); 42 43self.addEventListener("activate", (event) => { 44 console.log(`Service Worker ${OFFLINE_VERSION} activating...`); 45 46 event.waitUntil( 47 (async () => { 48 try { 49 // Delete old caches 50 const cacheNames = await caches.keys(); 51 await Promise.all( 52 cacheNames 53 .filter( 54 (cacheName) => 55 cacheName.startsWith("offline-") && cacheName !== CACHE_NAME 56 ) 57 .map((cacheName) => { 58 console.log("Deleting old cache:", cacheName); 59 return caches.delete(cacheName); 60 }) 61 ); 62 63 // Enable navigation preload if it's supported. 64 // See https://developers.google.com/web/updates/2017/02/navigation-preload 65 if ("navigationPreload" in self.registration) { 66 await self.registration.navigationPreload.enable(); 67 console.log("Navigation preload enabled"); 68 } 69 } catch (error) { 70 console.error("Activation failed:", error); 71 } 72 })() 73 ); 74 75 // Tell the active service worker to take control of the page immediately. 76 self.clients.claim(); 77}); 78 79self.addEventListener("fetch", (event) => { 80 // We only want to call event.respondWith() if this is a navigation request 81 // for an HTML page. 82 if (event.request.mode === "navigate") { 83 event.respondWith( 84 (async () => { 85 try { 86 // First, try to use the navigation preload response if it's supported. 87 const preloadResponse = await event.preloadResponse; 88 if (preloadResponse) { 89 return preloadResponse; 90 } 91 92 // Always try the network first. 93 const networkResponse = await fetch(event.request); 94 return networkResponse; 95 } catch (error) { 96 // catch is only triggered if an exception is thrown, which is likely 97 // due to a network error. 98 // If fetch() returns a valid HTTP response with a response code in 99 // the 4xx or 5xx range, the catch() will NOT be called. 100 console.log("Fetch failed; returning offline page instead.", error); 101 102 try { 103 const cache = await caches.open(CACHE_NAME); 104 const cachedResponse = await cache.match(OFFLINE_URL); 105 if (cachedResponse) { 106 return cachedResponse; 107 } else { 108 console.error("Offline page not found in cache"); 109 return new Response("Offline page not available", { 110 status: 503, 111 statusText: "Service Unavailable", 112 }); 113 } 114 } catch (cacheError) { 115 console.error("Cache access failed:", cacheError); 116 return new Response("Service temporarily unavailable", { 117 status: 503, 118 statusText: "Service Unavailable", 119 }); 120 } 121 } 122 })() 123 ); 124 } 125 126 // If our if() condition is false, then this fetch handler won't intercept the 127 // request. If there are any other fetch handlers registered, they will get a 128 // chance to call event.respondWith(). If no fetch handlers call 129 // event.respondWith(), the request will be handled by the browser as if there 130 // were no service worker involvement. 131}); 132 133// Handle service worker updates 134self.addEventListener("message", (event) => { 135 if (event.data && event.data.type === "SKIP_WAITING") {
136 self.skipWaiting(); 137 } 138}); 139 140// Log when service worker is ready 141console.log(`Service Worker ${OFFLINE_VERSION} loaded successfully`);
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.