ADR 0003: VAPID self-hosted today, DeviceToken model agnostic to
platform so iOS/Android (Expo/APNs/FCM) plug in as new senders later.
Backend (apps/api/src/modules/notifications)
- sender.ts: NotificationSender interface, WebPushSender via VAPID
- notifications.routes.ts: GET /me/notifications/config, POST/DELETE
/me/device-tokens, PATCH /me/notifications/settings, POST /me/notifications/test
- scheduler.ts: node-cron Mon 09:00 Europe/Rome for weekly weigh-in
reminder; auto-cleanup of expired tokens on 404/410
- env: VAPID_PUBLIC_KEY/PRIVATE_KEY/SUBJECT (all optional → push gracefully off)
Frontend
- public/sw.js minimal (push + notificationclick)
- lib/notifications/push-client.ts: subscribe / unsubscribe / getCurrentSubscription
- profile/notifications-{actions,panel}.tsx: editorial panel with toggles,
device list, "send test", per-device removal
- pushReady requires both permission AND active subscription (covers the
case where the user revoked the SW but kept the browser permission)
Schema
- DeviceToken { userId, platform, endpoint, p256dh, auth, token, userAgent,
createdAt, lastSeenAt } with unique(userId, endpoint)
- ExtendedPrismaClient type exported from @ketopath/db
- NotificationSettings zod schema in @ketopath/shared
Tooling
- lint-staged: split .js out of eslint glob so service worker is only
formatted (it lives outside the TS project)
i18n
- Notifications namespace (it) with typed error keys
Smoke tested: POST /me/device-tokens 201, POST /me/notifications/test 200,
real push delivered to a macOS Chrome device.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
// KetoPath service worker — minimo necessario per Web Push (PRD §5.6).
|
|
// Niente caching offline complesso: solo gestione `push` e `notificationclick`.
|
|
|
|
self.addEventListener('install', (event) => {
|
|
// attiva subito senza aspettare i tab vecchi
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
self.addEventListener('push', (event) => {
|
|
if (!event.data) return;
|
|
let payload;
|
|
try {
|
|
payload = event.data.json();
|
|
} catch (_err) {
|
|
payload = { title: 'KetoPath', body: event.data.text() };
|
|
}
|
|
const title = payload.title || 'KetoPath';
|
|
const options = {
|
|
body: payload.body || '',
|
|
icon: '/icons/icon-192.png',
|
|
badge: '/icons/badge-72.png',
|
|
data: { url: payload.url || '/' },
|
|
// niente tag → l'utente vede ogni notifica separata
|
|
};
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
const url = (event.notification.data && event.notification.data.url) || '/';
|
|
event.waitUntil(
|
|
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windowClients) => {
|
|
for (const client of windowClients) {
|
|
// se c'è già una tab aperta sull'app, focus + navigate
|
|
if ('focus' in client) {
|
|
client.navigate(url).catch(() => {
|
|
/* navigate non sempre supportato */
|
|
});
|
|
return client.focus();
|
|
}
|
|
}
|
|
if (self.clients.openWindow) {
|
|
return self.clients.openWindow(url);
|
|
}
|
|
return undefined;
|
|
}),
|
|
);
|
|
});
|