Skip to content

Commit

Permalink
Update sw.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bithalo committed Sep 7, 2024
1 parent 38f5e32 commit f150a44
Showing 1 changed file with 19 additions and 73 deletions.
92 changes: 19 additions & 73 deletions sw.js
Original file line number Diff line number Diff line change
@@ -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));
});

0 comments on commit f150a44

Please sign in to comment.