Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shorter way of deleting old cache #660

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions service-worker/selective-caching/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,13 @@ var CURRENT_CACHES = {
font: 'font-cache-v' + CACHE_VERSION
};

self.addEventListener('activate', function(event) {
// Delete all caches that aren't named in CURRENT_CACHES.
self.addEventListener('activate', async event => {
// Delete all caches that aren't named in CURRENT_CACHES.
// While there is only one cache in this example, the same logic will handle the case where
// there are multiple versioned caches.
var expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES));
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (!expectedCacheNamesSet.has(cacheName)) {
// If this cache name isn't present in the set of "expected" cache names, then delete it.
console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
const keys = await caches.keys();
keys.map(key =>key!=CURRENT_CACHES.font? caches.delete(key):'');
})

self.addEventListener('fetch', function(event) {
console.log('Handling fetch event for', event.request.url);
Expand Down