Skip to content

Instantly share code, notes, and snippets.

@Chalarangelo
Created July 17, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chalarangelo/7a95ea85561c924a9751df79ec55eb65 to your computer and use it in GitHub Desktop.
Save Chalarangelo/7a95ea85561c924a9751df79ec55eb65 to your computer and use it in GitHub Desktop.
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