Cache assets when cache is missing

This commit is contained in:
tusooa 2024-08-23 02:49:07 -04:00
commit 8b8f6cbca2
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51

View file

@ -96,14 +96,16 @@ const isEmoji = req => {
return url.pathname.startsWith('/emoji/')
}
const isNotMedia = req => {
console.log('req.method=', req.method)
if (req.method !== 'GET') {
return false
}
const url = new URL(req.url)
console.log('pathname=', url.pathname)
return !url.pathname.startsWith('/media/')
}
const isAsset = req => {
const url = new URL(req.url)
return cacheFiles.includes(url.pathname)
}
const isSuccessful = (resp) => {
if (!resp.ok) {
@ -216,17 +218,23 @@ self.addEventListener('fetch', (event) => {
// Do not mess up with remote things
const isSameOrigin = (new URL(event.request.url)).origin === self.location.origin
if (shouldCache && event.request.method === 'GET' && isSameOrigin && isNotMedia(event.request)) {
console.debug('[Service worker] fetch:', event.request.url)
event.respondWith((async () => {
const r = await caches.match(event.request)
const isEmojiReq = isEmoji(event.request)
if (r && (isSuccessful(r) || !isEmoji(event.request))) {
if (r && isSuccessful(r)) {
console.debug('[Service worker] already cached:', event.request.url)
return r
}
try {
const response = await fetch(event.request)
if (response.ok && isSuccessful(response) && isEmoji(event.request)) {
const cache = await caches.open(emojiCacheKey)
if (response.ok &&
isSuccessful(response) &&
(isEmojiReq || isAsset(event.request))) {
console.debug(`[Service worker] caching ${isEmojiReq ? 'emoji' : 'asset'}:`, event.request.url)
const cache = await caches.open(isEmojiReq ? emojiCacheKey : cacheKey)
await cache.put(event.request.clone(), response.clone())
}
return response