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 = "/pwa/app/views/offline.htm"; 23var urlsToCache = [ 24 '/pwa/assets/css/offline.min.css', 25 '/pwa/assets/images/no_internet.png', 26 '/pwa/assets/js/offline.js', 27 '/pwa/assets/js/offline.min.js', 28 '/pwa/assets/sass/offline.scss' 29]; 30self.addEventListener("install", (event) => { 31 event.waitUntil( 32 (async () => { 33 const cache = await caches.open(CACHE_NAME); 34 // Setting {cache: 'reload'} in the new request will ensure that the 35 // response isn't fulfilled from the HTTP cache; i.e., it will be from 36 // the network. 37 38 await cache.addAll(urlsToCache); 39 await cache.add(new Request(OFFLINE_URL, { cache: "reload" })); 40 })() 41 ); 42 // Force the waiting service worker to become the active service worker. 43 self.skipWaiting(); 44}); 45 46self.addEventListener("activate", (event) => { 47 event.waitUntil( 48 (async () => { 49 // Enable navigation preload if it's supported. 50 // See https://developers.google.com/web/updates/2017/02/navigation-preload 51 if ("navigationPreload" in self.registration) { 52 await self.registration.navigationPreload.enable(); 53 } 54 })() 55 ); 56 57 // Tell the active service worker to take control of the page immediately. 58 self.clients.claim(); 59}); 60 61self.addEventListener("fetch", (event) => { 62 // We only want to call event.respondWith() if this is a navigation request 63 // for an HTML page. 64 65 if (event.request.mode === "navigate") { 66 event.respondWith( 67 (async () => { 68 try { 69 // First, try to use the navigation preload response if it's supported. 70 const preloadResponse = await event.preloadResponse; 71 if (preloadResponse) { 72 return preloadResponse; 73 } 74 75 // Always try the network first. 76 const networkResponse = await fetch(event.request); 77 return networkResponse; 78 } catch (error) { 79 // catch is only triggered if an exception is thrown, which is likely 80 // due to a network error. 81 // If fetch() returns a valid HTTP response with a response code in 82 // the 4xx or 5xx range, the catch() will NOT be called. 83 console.log("Fetch failed; returning offline page instead.", error); 84 85 const cache = await caches.open(CACHE_NAME); 86 const cachedResponse = await cache.match(OFFLINE_URL); 87 return cachedResponse; 88 } 89 })() 90 ); 91 }else{ 92 93 event.respondWith( 94 caches.match(event.request) 95 .then(function(response) { 96 // Cache hit - return response 97 if (response) { 98 return response; 99 } 100 try { 101 return fetch(event.request).catch(function(err) { // fallback mechanism 102 console.log("fetch error"+err); 103 return null; 104 105 }); 106 107 108 ; 109 } catch (error) {} 110 } 111 ) 112 ); 113 114 } 115 116 // If our if() condition is false, then this fetch handler won't intercept the 117 // request. If there are any other fetch handlers registered, they will get a 118 // chance to call event.respondWith(). If no fetch handlers call 119 // event.respondWith(), the request will be handled by the browser as if there 120 // were no service worker involvement. 121});
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.