1const SW = { 2 OFFLINE_PAGE: "/static/offline-dummy.html", 3 CACHE_NAME: null, 4 HOST: ['www.primeapi.com', 'qa.primeapi.com'] 5}; 6 7// Install stage sets up the index page (home page) in the cache and opens a new cache 8self.addEventListener("install", function (event) { 9 const url = new URL(location); 10 const v = url.searchParams.get('v'); 11 const brandCode = url.searchParams.get('brand_code'); 12 13 if (!v || !brandCode) { 14 console.error("Service worker missing params"); 15 return; 16 } 17 18 SW.CACHE_NAME = `${brandCode}.${v}`; 19 SW.HOST.push(url.hostname); 20 21 event.waitUntil( 22 caches.open(SW.CACHE_NAME).then(function (cache) { 23 return cache.addAll( 24 [ 25 SW.OFFLINE_PAGE 26 ] 27 ); 28 }) 29 ); 30}); 31 32self.addEventListener('fetch', function (event) { 33 const { request } = event; 34 const url = new URL(event.request.url); 35 // Prevent Chrome Developer Tools error: 36 // Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode 37 // 38 // See also https://stackoverflow.com/a/49719964/1217468 39 if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { 40 return; 41 } 42 43 if (request.method !== 'GET' || SW.HOST.indexOf(url.hostname) === -1 || url.pathname.indexOf("/lp/") === -1 || url.pathname.startsWith('/son-api') || url.searchParams.get('no-cache') !== null) { 44 return; 45 } 46 47 event.respondWith(caches.open(SW.CACHE_NAME).then((cache) => { 48 // Respond with the image from the cache or from the network 49 return cache.match(event.request).then((cachedResponse) => { 50 return cachedResponse || fetch(event.request.url).then((fetchedResponse) => { 51 // Add the network response to the cache for future visits. 52 // Note: we need to make a copy of the response to save it in 53 // the cache and use the original as the request response. 54 cache.put(event.request, fetchedResponse.clone()); 55 56 // Return the network response 57 return fetchedResponse; 58 }); 59 }); 60 })); 61}); 62 63 64self.addEventListener('activate', event => { 65 // Clean up caches other than current. 66 event.waitUntil(async function () { 67 const cacheNames = await caches.keys(); 68 69 await Promise.all( 70 cacheNames.filter((cacheName) => { 71 const deleteThisCache = cacheName !== SW.CACHE_NAME; 72 return deleteThisCache; 73 }).map(cacheName => caches.delete(cacheName)) 74 ); 75 }()); 76});
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.