Skip to content

Commit

Permalink
Add cache strategy with cacheFirst and networkFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
annsa9 committed May 24, 2020
1 parent 1d5be01 commit c0a03c3
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,42 @@ self.addEventListener('fetch', (event) => {
console.info('Event: Fetch');

var request = event.request;
var url = new URL(request.url);
if (url.origin === location.origin) {
// Static files cache
event.respondWith(cacheFirst(request));
} else {
// Dynamic API cache
event.respondWith(networkFirst(request));
}

//Tell the browser to wait for newtwork request and respond with below
event.respondWith(
//If request is already in cache, return it
caches.match(request).then((response) => {
if (response) {
return response;
}

// // Checking for navigation preload response
// if (event.preloadResponse) {
// console.info('Using navigation preload');
// return response;
// }

//if request is not cached or navigation preload response, add it to cache
return fetch(request).then((response) => {
var responseToCache = response.clone();
caches.open(cacheName).then((cache) => {
cache.put(request, responseToCache).catch((err) => {
console.warn(request.url + ': ' + err.message);
});
});

return response;
});
})
);
// // Checking for navigation preload response
// if (event.preloadResponse) {
// console.info('Using navigation preload');
// return response;
// }
});

async function cacheFirst(request) {
const cachedResponse = await caches.match(request);
return cachedResponse || fetch(request);
}

async function networkFirst(request) {
const dynamicCache = await caches.open(cacheName);
try {
const networkResponse = await fetch(request);
// Cache the dynamic API response
dynamicCache.put(request, networkResponse.clone()).catch((err) => {
console.warn(request.url + ': ' + err.message);
});
return networkResponse;
} catch (err) {
const cachedResponse = await dynamicCache.match(request);
return cachedResponse;
}
}

/*
ACTIVATE EVENT: triggered once after registering, also used to clean up caches.
*/
Expand Down

0 comments on commit c0a03c3

Please sign in to comment.