Change SW cache technique

This commit is contained in:
Marcus Noble 2017-10-15 07:24:21 +01:00
parent 09e95c1b65
commit d2213990e1

View File

@ -6,13 +6,13 @@ self.addEventListener('fetch', function(event) {
function fetchAndCache(event) {
return caches.open(CACHE).then(function (cache) {
return fetch(event.request)
.then(function(response) {
cache.put(event.request, response.clone());
return response;
})
.catch(function() {
return cache.match(event.request);
return cache.match(event.request).then(response => {
var fetchResponse = fetch(event.request)
.then(function(networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchResponse;
});
});
}