Created
July 17, 2017 13:35
-
-
Save Chalarangelo/7a95ea85561c924a9751df79ec55eb65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('activate', function(e) { | |
e.waitUntil(caches.keys() | |
.then(function(keyList) { | |
return Promise.all(keyList.map(function(key) { | |
if (key !== cacheName) | |
return caches.delete(key); | |
})); | |
})); | |
return self.clients.claim(); | |
}); | |
self.addEventListener('fetch', function(e) { | |
e.respondWith(caches.match(e.request) | |
.then(function(response) { | |
return response || fetch(e.request) | |
.then(function (resp){ | |
return caches.open(cacheName) | |
.then(function(cache){ | |
cache.put(e.request, resp.clone()); | |
return resp; | |
}) | |
}).catch(function(event){ | |
console.log('Error fetching data!'); | |
}) | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment