// Service worker Arboretum (P4-C) : Web Push + clic de notification. // Volontairement minimal, pas de précache offline (hors périmètre P4) : son seul rôle est de // recevoir les push (session passée en `waiting`) et d'ouvrir/focus la vue session au clic. self.addEventListener('push', (event) => { let data = {}; try { data = event.data ? event.data.json() : {}; } catch { data = {}; } const title = data.title || 'Arboretum'; const options = { body: data.body || 'A session needs your input.', tag: data.sessionId || 'arboretum', // remplace la notif précédente de la même session data: { url: data.url || '/' }, icon: '/icon.svg', badge: '/icon.svg', requireInteraction: true, // ignoré sur iOS, respecté ailleurs }; event.waitUntil(self.registration.showNotification(title, options)); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); const url = (event.notification.data && event.notification.data.url) || '/'; const target = new URL(url, self.location.origin).href; event.waitUntil( (async () => { const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true }); for (const client of clients) { // un onglet de l'app est déjà ouvert : on le focus (et on y navigue si besoin) if ('focus' in client) { await client.focus(); if (client.url !== target && 'navigate' in client) { try { await client.navigate(target); } catch { // navigate peut échouer selon le contexte ; le focus suffit } } return; } } await self.clients.openWindow(target); })(), ); }); // Pas de précache : on s'active immédiatement pour que le push soit opérationnel sans recharger. self.addEventListener('install', () => self.skipWaiting()); self.addEventListener('activate', (event) => event.waitUntil(self.clients.claim()));