From 460b741f171bccb01326b5effb3a41f2d07c4776 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Thu, 5 Sep 2019 15:24:26 -0700 Subject: [PATCH] clean service worker cache after activate instead of on install --- app/serviceWorker.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/serviceWorker.js b/app/serviceWorker.js index cc709bab..8ab9e850 100644 --- a/app/serviceWorker.js +++ b/app/serviceWorker.js @@ -17,7 +17,7 @@ self.addEventListener('install', event => { }); self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()); + event.waitUntil(self.clients.claim().then(cleanCache)); }); async function decryptStream(id) { @@ -83,16 +83,19 @@ async function decryptStream(id) { } 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(); for (const c of oldCaches) { if (c !== version) { 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) {