clean service worker cache after activate instead of on install

This commit is contained in:
Danny Coates 2019-09-05 15:24:26 -07:00
parent d5c488196d
commit 460b741f17
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 8 additions and 5 deletions

View File

@ -17,7 +17,7 @@ self.addEventListener('install', event => {
}); });
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim().then(cleanCache));
}); });
async function decryptStream(id) { async function decryptStream(id) {
@ -83,16 +83,19 @@ async function decryptStream(id) {
} }
async function precache() { async function precache() {
const cache = await caches.open(version);
const images = assets.match(IMAGES);
await cache.addAll(images);
return self.skipWaiting();
}
async function cleanCache() {
const oldCaches = await caches.keys(); const oldCaches = await caches.keys();
for (const c of oldCaches) { for (const c of oldCaches) {
if (c !== version) { if (c !== version) {
await caches.delete(c); await caches.delete(c);
} }
} }
const cache = await caches.open(version);
const images = assets.match(IMAGES);
await cache.addAll(images);
return self.skipWaiting();
} }
async function cachedOrFetched(req) { async function cachedOrFetched(req) {