Created
March 11, 2018 23:52
-
-
Save ritikrishu/9e7f5e3b0825aaeac2b811c783327c72 to your computer and use it in GitHub Desktop.
remove old cache when service worker is updated
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
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