wait until serviceWorker activate to precache

This commit is contained in:
Danny Coates 2019-09-05 19:04:03 -07:00
parent 94f1eabbc7
commit 883f2bc0f9
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB

View file

@ -13,12 +13,12 @@ const VERSIONED_ASSET = /\.[A-Fa-f0-9]{8}\.(js|css|png|svg|jpg)$/;
const DOWNLOAD_URL = /\/api\/download\/([A-Fa-f0-9]{4,})/;
const FONT = /\.woff2?$/;
self.addEventListener('install', event => {
event.waitUntil(precache());
self.addEventListener('install', () => {
self.skipWaiting();
});
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim().then(cleanCache));
event.waitUntil(self.clients.claim().then(precache));
});
async function decryptStream(id) {
@ -84,10 +84,15 @@ async function decryptStream(id) {
}
async function precache() {
try {
await cleanCache();
const cache = await caches.open(version);
const images = assets.match(IMAGES);
await cache.addAll(images);
return self.skipWaiting();
} catch (e) {
console.error(e);
// cache will get populated on demand
}
}
async function cleanCache() {