From 61d072cb063acb67679b522b91b6d77737d88447 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Thu, 13 Jun 2024 12:27:56 +0200 Subject: [PATCH] feat(pwa): :sparkles: add service worker module --- src/serviceworker/mod.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/serviceworker/mod.ts b/src/serviceworker/mod.ts index c910317..3700c87 100644 --- a/src/serviceworker/mod.ts +++ b/src/serviceworker/mod.ts @@ -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 } + 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