feat(pwa): add service worker module

This commit is contained in:
Julien Oculi 2024-06-13 12:27:56 +02:00
parent 6ae5348e2e
commit 61d072cb06

View file

@ -9,22 +9,31 @@ const _preCachedPaths = ['/', '/css/*', '/assets/*'] //TODO pre-cache these path
export function main() {
self.addEventListener('install', (event) => {
//TODO handle installation
event.waitUntil(
addToCache([]),
)
})
self.addEventListener('activate', () => {
console.log('sw activated')
//TODO handle activation
})
self.addEventListener('fetch', (_event) => {
//TODO add fetch strategies
})
self.addEventListener('push', (event) => {
const { title, options } = (event.data?.json() ?? {}) as { title?: string, options?: Partial<NotificationOptions> }
if (title) {
event.waitUntil(
self.registration.showNotification(title, options)
)
}
})
}
async function addToCache(ressources: string[]) {
const cache = await caches.open(cacheName) //TODO dynamique cache key
await cache.addAll(ressources)
//TODO list statics