From 7c9823e0790df826a2743e91e8f8126bdc67e97c Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Sun, 15 Aug 2021 10:51:49 -0400 Subject: [PATCH] Cache emojis in service worker --- src/sw.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sw.js b/src/sw.js index 23a63c38..c8baad55 100644 --- a/src/sw.js +++ b/src/sw.js @@ -52,6 +52,16 @@ const maybeShowNotification = async (event) => { const shouldCache = process.env.NODE_ENV === 'production' const cacheKey = 'pleroma-fe' 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) => { if (shouldCache) { @@ -105,7 +115,13 @@ self.addEventListener('fetch', async (event) => { if (r) { 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()) + } return response })()) }