Skip to content

Instantly share code, notes, and snippets.

@ritikrishu
Created March 11, 2018 23:52
Show Gist options
  • Save ritikrishu/9e7f5e3b0825aaeac2b811c783327c72 to your computer and use it in GitHub Desktop.
Save ritikrishu/9e7f5e3b0825aaeac2b811c783327c72 to your computer and use it in GitHub Desktop.
remove old cache when service worker is updated
const prefix = 'version-dependent';
const version = 1.0.0;
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.filter(function (cacheName) {
if(cacheName.startsWith(prefix)){
if(cacheName.endsWith(version){
return false;
}
return true;
}
return false;
}).map(function (cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment