Files
bobinoo/public/push-sw.js
T
Claude 6f7064f53f Notifications push (Web Push) et indicateur hors-ligne / synchro en attente
- Réglages → Notifications : interrupteur « Notifications sur cet appareil »
  (permission demandée au clic uniquement, iOS : explication « installe l'app »,
  permission refusée, navigateur non compatible). Désinscription à la déconnexion.
- Service worker : push-sw.js chargé via workbox.importScripts (affichage +
  clic qui ouvre/focalise l'app sur le bon écran via « /?open=… »).
- Serveur : table push_subscriptions (RLS, anti-SSRF, 10 appareils max),
  déclencheur pg_net + secret dans Vault, fonction send-push (aes128gcm +
  VAPID ES256 en WebCrypto, testée sur le vecteur RFC 8291), regroupement des
  épisodes, préférences notifPrefs, ménage des abonnements expirés.
- scripts/gen-vapid.mjs et supabase/PUSH_SETUP.md (étapes manuelles).
- Pastille discrète hors-ligne / « N modifications en attente » (mobile au-dessus
  de la barre de nav, ordinateur dans la barre latérale), basée sur l'état du
  moteur de synchro existant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-10-09 15:21:17 +00:00

73 lines
2.6 KiB
JavaScript

/* AzuuCine — notifications push (chargé dans le service worker généré par Workbox via importScripts).
* Affiche la notification reçue, et au clic ouvre (ou ramène au premier plan) l'app sur le bon écran. */
/* eslint-env serviceworker */
// Écrans que le serveur peut demander d'ouvrir (tout le reste ouvre l'accueil)
var OPEN_RE = /^(notifications|item:[A-Za-z0-9_-]{1,64}|u:[a-z0-9_]{3,20})$/
function clip(v, max) {
if (typeof v !== 'string') return ''
v = v.replace(/[\u0000-\u001f\u007f]/g, ' ').trim()
return v.length > max ? v.slice(0, max - 1) + '…' : v
}
/** Adresse à ouvrir : uniquement « /?open=<écran> » sur ce site. */
function safeUrl(raw) {
try {
var u = new URL(typeof raw === 'string' ? raw : '/', self.location.origin)
var open = u.searchParams.get('open') || ''
if (u.origin === self.location.origin && u.pathname === '/' && OPEN_RE.test(open)) return '/?open=' + encodeURIComponent(open)
} catch (e) {
/* adresse invalide */
}
return '/'
}
self.addEventListener('push', function (event) {
var data = {}
try {
data = event.data ? event.data.json() : {}
} catch (e) {
data = {}
}
var title = clip(data.title, 120) || 'AzuuCine'
var options = {
body: clip(data.body, 300),
tag: clip(data.tag, 64) || undefined,
icon: '/icons/icon-192.png',
data: { url: safeUrl(data.url) },
}
event.waitUntil(
Promise.all([
self.registration.showNotification(title, options),
// App ouverte : on lui signale qu'il y a du nouveau (compteur de la cloche)
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
list.forEach(function (c) {
c.postMessage({ type: 'azuu:push' })
})
}),
]),
)
})
self.addEventListener('notificationclick', function (event) {
event.notification.close()
var url = safeUrl(event.notification.data && event.notification.data.url)
var open = new URL(url, self.location.origin).searchParams.get('open') || ''
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
// Une fenêtre de l'app est déjà ouverte : on la ramène et on lui dit quoi afficher
for (var i = 0; i < list.length; i++) {
var c = list[i]
var cu = new URL(c.url)
// (les pages légales, /privacy et /legal, ne sont pas l'app)
if (cu.origin === self.location.origin && cu.pathname === '/' && 'focus' in c) {
c.postMessage({ type: 'azuu:open', open: open })
return c.focus()
}
}
return self.clients.openWindow ? self.clients.openWindow(url) : undefined
}),
)
})