Files
arboretum/packages/web/public/sw.js
Johan LEROY 1b4c19e00a P4-C: PWA installable, service worker push & campagne de fiabilité dialogues
- PWA: public/manifest.webmanifest + icon.svg + meta index.html (theme-color, apple-touch-icon)
- public/sw.js: service worker push-only (push → showNotification tag=sessionId ;
  notificationclick → focus/ouvre /sessions/<id>) — pas de précache (hors périmètre)
- lib/push.ts + stores/push.ts: enable/disable, abonnement VAPID, enregistrement SW au boot
- DashboardView: toggle « Notifications » (gardé par pushSupported) + i18n en/fr
- campagne de fiabilité (verdict S3): classification de tous les types de dialogue sur captures
  réelles (trust/permission×2/question) + refus Esc (deny-esc2) + plan (synthétique)
- acceptance-p4.mjs: VAPID/auth/Origin, subscribe idempotent/unsubscribe, answer (rejets)

Choix: SW écrit à la main plutôt que vite-plugin-pwa (qui tirait ~295 paquets Workbox +
2 vulns esbuild high pour un SW push-only). Zéro dépendance front nouvelle. 175 tests verts,
acceptance p1..p4 ALL GREEN.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 12:23:00 +02:00

53 lines
2.0 KiB
JavaScript

// 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()));