From f150a44653409e132409380d891385d848ea691e Mon Sep 17 00:00:00 2001 From: bithalo <> Date: Sat, 7 Sep 2024 14:26:27 -0700 Subject: [PATCH] Update sw.js --- sw.js | 92 ++++++++++++----------------------------------------------- 1 file changed, 19 insertions(+), 73 deletions(-) diff --git a/sw.js b/sw.js index 8dcebe3..22381e2 100644 --- a/sw.js +++ b/sw.js @@ -1,84 +1,30 @@ -/* - Cache Strategy: cacheAll (boolean flag) - - 1) cacheAll = true => Cache all requests in urlsToCache list and all further requests - 2) cacheAll = false => Cache only all requests in urlsToCache list -*/ -var cacheAll = true; -var CACHE_NAME = 'webapk-cache'; -var urlsToCache = [ - //'./index.html', - //'./all.min.css', - //'./axios.min.js', - //'./bignumber.min.js', - //'./cryptico.min.js', - //'./crypto-js.js', - //'./crypto-sha256.js', - //'./DDEABI.js', - //'./ERC20.js', - //'./faq.js', - //'./jdenticon.min.js', - //'./lottie-player.js', - //'./open-sans.css', - //'./purify.js', - //'./simple-scrollbar.css', - //'./simple-scrollbar.min.js', - //'./sweetalert211.js', - //'./translate.js', - //'./web3.min.js' -]; - -var urlsNotToCache = [ - // Urls that don't need to be cached can be added here explicitly -]; +// Service Worker // Install Event self.addEventListener('install', function(event) { - console.log("[SW] install event: ",event); - // Perform install steps + console.log("[SW] Install event"); + // No caching during install +}); + +// Activate Event +self.addEventListener('activate', function(event) { + console.log("[SW] Activate event"); + // No action needed, but you can clear old caches if they exist event.waitUntil( - caches.open(CACHE_NAME).then( - function(cache) { - console.log('[SW] Opened cache: ',cache); - return cache.addAll(urlsToCache); - } - ) + caches.keys().then(function(cacheNames) { + return Promise.all( + cacheNames.map(function(cacheName) { + return caches.delete(cacheName); + }) + ); + }) ); }); - // Fetch Event self.addEventListener('fetch', function(event) { - console.log("[SW] fetch event: ",event); - event.respondWith( - caches.match(event.request).then( - function(response) { - // Cache hit - return response - if (response) return response; - // What cache strategy should be used? => cacheAll (boolean flag) - else if (!cacheAll || urlsNotToCache.indexOf(event.request) !== -1) return fetch(event.request); - else { - fetch(event.request).then( - // Try to cache new requests directly - function(response) { - // Check if we received a valid response - if(!response || response.status !== 200 || response.type !== 'basic') return response; - // IMPORTANT: Clone the response. A response is a stream - // and because we want the browser to consume the response - // as well as the cache consuming the response, we need - // to clone it so we have two streams. - var responseToCache = response.clone(); - caches.open(CACHE_NAME).then( - function(cache) { - cache.put(event.request, responseToCache); - } - ); - return response; - } - ); - } - } - ) - ); + console.log("[SW] Fetch event: ", event.request.url); + // Just pass through the request without caching + event.respondWith(fetch(event.request)); });