Cache compiled assets in service worker
This commit is contained in:
parent
a959104333
commit
2b16dd55c4
1 changed files with 42 additions and 1 deletions
43
src/sw.js
43
src/sw.js
|
|
@ -85,6 +85,35 @@ const showPushNotification = async (event) => {
|
|||
return Promise.resolve()
|
||||
}
|
||||
|
||||
const shouldCache = process.env.NODE_ENV === 'production'
|
||||
const cacheKey = 'pleroma-fe'
|
||||
const cacheFiles = self.serviceWorkerOption.assets
|
||||
|
||||
self.addEventListener('install', async (event) => {
|
||||
if (shouldCache) {
|
||||
event.waitUntil((async () => {
|
||||
const cache = await caches.open(cacheKey)
|
||||
await cache.addAll(cacheFiles)
|
||||
})())
|
||||
}
|
||||
})
|
||||
|
||||
self.addEventListener('activate', async (event) => {
|
||||
if (shouldCache) {
|
||||
event.waitUntil((async () => {
|
||||
const cache = await caches.open(cacheKey)
|
||||
const keys = await cache.keys()
|
||||
await Promise.all(
|
||||
keys.filter(request => {
|
||||
const url = new URL(request.url)
|
||||
const shouldKeep = cacheFiles.includes(url.pathname)
|
||||
return !shouldKeep
|
||||
}).map(k => cache.delete(k))
|
||||
)
|
||||
})())
|
||||
}
|
||||
})
|
||||
|
||||
self.addEventListener('push', async (event) => {
|
||||
if (event.data) {
|
||||
// Supposedly, we HAVE to return a promise inside waitUntil otherwise it will
|
||||
|
|
@ -143,4 +172,16 @@ self.addEventListener('notificationclick', (event) => {
|
|||
}))
|
||||
})
|
||||
|
||||
self.addEventListener('fetch', _ => _)
|
||||
self.addEventListener('fetch', async (event) => {
|
||||
if (shouldCache) {
|
||||
event.respondWith((async () => {
|
||||
const r = await caches.match(event.request)
|
||||
console.log(`[Service Worker] Fetching resource: ${event.request.url}`)
|
||||
if (r) {
|
||||
return r
|
||||
}
|
||||
const response = await fetch(event.request)
|
||||
return response
|
||||
})())
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue