website/src/serviceworker/mod.ts
2024-06-13 12:43:29 +02:00

44 lines
1 KiB
TypeScript

/// <reference no-default-lib="true"/>
/// <reference lib="webworker" />
// Force load service worker types
const self = globalThis as unknown as ServiceWorkerGlobalScope
const cacheName = 'v1' //TODO dynamique cache key
const _preCachedPaths = ['/', '/css/*', '/assets/*'] //TODO pre-cache these paths
export function main() {
self.addEventListener('install', (event) => {
//TODO handle installation
event.waitUntil(
addToCache([]),
)
})
self.addEventListener('activate', () => {
//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
}