cache fonts

This commit is contained in:
Danny Coates 2019-09-05 15:57:07 -07:00
parent 460b741f17
commit 902bc6628e
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 7 additions and 2 deletions

View File

@ -11,6 +11,7 @@ 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,})/;
const FONT = /\.woff2?$/;
self.addEventListener('install', event => {
event.waitUntil(precache());
@ -98,6 +99,10 @@ async function cleanCache() {
}
}
function cacheable(url) {
return VERSIONED_ASSET.test(url) || FONT.test(url);
}
async function cachedOrFetched(req) {
const cache = await caches.open(version);
const cached = await cache.match(req);
@ -105,7 +110,7 @@ async function cachedOrFetched(req) {
return cached;
}
const fetched = await fetch(req);
if (fetched.ok && VERSIONED_ASSET.test(req.url)) {
if (fetched.ok && cacheable(req.url)) {
cache.put(req, fetched.clone());
}
return fetched;
@ -118,7 +123,7 @@ self.onfetch = event => {
const dlmatch = DOWNLOAD_URL.exec(url.pathname);
if (dlmatch) {
event.respondWith(decryptStream(dlmatch[1]));
} else if (VERSIONED_ASSET.test(url.pathname)) {
} else if (cacheable(url.pathname)) {
event.respondWith(cachedOrFetched(req));
}
};