Changed to network-first caching

This commit is contained in:
Marcus Noble 2018-01-26 18:56:01 +00:00
parent 4fb38ae95a
commit cca58fa179

View File

@ -1,4 +1,8 @@
var CACHE = 'v2'; var CACHE = 'v3';
self.addEventListener('install', function() {
return self.skipWaiting();
})
self.addEventListener('fetch', function(event) { self.addEventListener('fetch', function(event) {
event.respondWith(fetchAndCache(event)); event.respondWith(fetchAndCache(event));
@ -18,13 +22,14 @@ function cacheBust(request) {
function fetchAndCache(event) { function fetchAndCache(event) {
return caches.open(CACHE).then(function (cache) { return caches.open(CACHE).then(function (cache) {
return cache.match(event.request).then(response => { return fetch(event.request)
var fetchResponse = fetch(cacheBust(event.request)) .then(function(networkResponse) {
.then(function(networkResponse) { cache.put(event.request, networkResponse.clone());
cache.put(event.request, networkResponse.clone()); return networkResponse;
return networkResponse; })
}); .catch(function(err) {
return response || fetchResponse; console.log(err)
return caches.match(event.request);
}); });
}); });
} }