improved serviceworker caching

This commit is contained in:
Danny Coates 2018-11-26 13:25:47 -08:00
parent bf2399ab88
commit 613a70f473
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 20 additions and 8 deletions

View File

@ -8,6 +8,9 @@ import contentDisposition from 'content-disposition';
let noSave = false; let noSave = false;
const map = new Map(); const map = new Map();
const IMAGES = /.*\.(png|svg|jpg)$/;
const VERSIONED_ASSET = /\.[A-Fa-f0-9]{8}\.(js|css|png|svg|jpg)$/;
const DOWNLOAD_URL = /\/api\/download\/([A-Fa-f0-9]{4,})/;
self.addEventListener('install', event => { self.addEventListener('install', event => {
event.waitUntil(precache()); event.waitUntil(precache());
@ -87,24 +90,33 @@ async function precache() {
} }
} }
const cache = await caches.open(version); const cache = await caches.open(version);
const images = assets.match(/.*\.(png|svg|jpg)$/); const images = assets.match(IMAGES);
await cache.addAll(images); await cache.addAll(images);
return self.skipWaiting(); return self.skipWaiting();
} }
async function cachedOrFetch(req) { async function cachedOrFetched(req) {
const cache = await caches.open(version); const cache = await caches.open(version);
const cached = await cache.match(req); const cached = await cache.match(req);
return cached || fetch(req); if (cached) {
return cached;
}
const fetched = await fetch(req);
if (fetched.ok && VERSIONED_ASSET.test(req.url)) {
cache.put(req, fetched.clone());
}
return fetched;
} }
self.onfetch = event => { self.onfetch = event => {
const req = event.request; const req = event.request;
const match = /\/api\/download\/([A-Fa-f0-9]{4,})/.exec(req.url); if (req.method !== 'GET') return;
if (match) { const url = new URL(req.url);
event.respondWith(decryptStream(match[1])); const dlmatch = DOWNLOAD_URL.exec(url.pathname); // TODO use #hashkey
} else { if (dlmatch) {
event.respondWith(cachedOrFetch(req)); event.respondWith(decryptStream(dlmatch[1]));
} else if (VERSIONED_ASSET.test(url.pathname)) {
event.respondWith(cachedOrFetched(req));
} }
}; };