Skip to content

Instantly share code, notes, and snippets.

@Himachallad
Created July 26, 2020 19:42
Show Gist options
  • Save Himachallad/8bfac63d24b9cffc67b47699198b13ac to your computer and use it in GitHub Desktop.
Save Himachallad/8bfac63d24b9cffc67b47699198b13ac to your computer and use it in GitHub Desktop.
/**
* Activates the new service worker and clean old cache incase of version changes
*/
self.addEventListener("activate", (event) => {
const currentCaches = [UNVARYING_CACHE, VOLATILE];
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter(
(cacheName) => !currentCaches.includes(cacheName)
);
})
.then((cachesToDelete) => {
return Promise.all(
cachesToDelete.map((cacheToDelete) => {
return caches.delete(cacheToDelete);
})
);
})
.then(() => self.clients.claim())
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment