Fix the bug where remote media fail to load without media proxy

This commit is contained in:
Tusooa Zhu 2022-01-11 23:03:46 -05:00 committed by Sam Therapy
parent 7c9823e079
commit 45e71af01e
Signed by: sam
GPG key ID: 4D8B07C18F31ACBD

View file

@ -107,8 +107,12 @@ self.addEventListener('notificationclick', (event) => {
}))
})
self.addEventListener('fetch', async (event) => {
if (shouldCache) {
self.addEventListener('fetch', (event) => {
console.log(`[Service Worker] Got: ${event.request.url}`)
console.debug(event.request)
// 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) {
event.respondWith((async () => {
const r = await caches.match(event.request)
console.log(`[Service Worker] Fetching resource: ${event.request.url}`)
@ -116,13 +120,18 @@ self.addEventListener('fetch', async (event) => {
return r
}
const response = await fetch(event.request)
if (response.ok && isEmoji(event.request)) {
console.log(`[Service Worker] Caching emoji ${event.request.url}`)
const cache = await caches.open(emojiCacheKey)
await cache.put(event.request.clone(), response.clone())
try {
const response = await fetch(event.request)
if (response.ok && isEmoji(event.request)) {
console.log(`[Service Worker] Caching emoji ${event.request.url}`)
const cache = await caches.open(emojiCacheKey)
await cache.put(event.request.clone(), response.clone())
}
return response
} catch (e) {
console.log('error:', e)
throw e
}
return response
})())
}
})