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
1 changed files with 12 additions and 7 deletions

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