Cache emojis in service worker

This commit is contained in:
Tusooa Zhu 2021-08-15 10:51:49 -04:00 committed by tusooa
commit 63490ad2da
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51

View file

@ -88,6 +88,16 @@ const showPushNotification = async (event) => {
const shouldCache = process.env.NODE_ENV === 'production' const shouldCache = process.env.NODE_ENV === 'production'
const cacheKey = 'pleroma-fe' const cacheKey = 'pleroma-fe'
const cacheFiles = self.serviceWorkerOption.assets const cacheFiles = self.serviceWorkerOption.assets
const emojiCacheKey = 'pleroma-fe-emoji'
const isEmoji = 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('/emoji/')
}
self.addEventListener('install', async (event) => { self.addEventListener('install', async (event) => {
if (shouldCache) { if (shouldCache) {
@ -180,7 +190,13 @@ self.addEventListener('fetch', async (event) => {
if (r) { if (r) {
return r return r
} }
const response = await fetch(event.request) 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 return response
})()) })())
} }