SkateSpots/public/push-sw.js
SkateSpots 38317ee2a5 feat(push): client half - subscribe flow, service worker, DM marker
- public/push-sw.js: shows the content-free push; tap focuses/opens the app.
- src/lib/pushService.ts + usePushNotifications: register the SW, subscribe with
  the service's VAPID key, sign a NIP-98 challenge proof, POST /subscribe.
- NotificationSettingsCard: a 'Background notifications' toggle (opt-in,
  per-device) + honest metadata copy; corrected the now-stale 'we run no push
  server' note.
- useSendDirectMessage: marks the RECIPIENT wrap ["skatespots","push"] (never
  the self copy) and adds the community relay as the notify relay so the push
  service sees it.
- /privacy: discloses the push server sees metadata only.
2026-10-01 15:39:04 +07:00

40 lines
1.3 KiB
JavaScript

/*
* SkateSpots push service worker.
*
* Receives a Web Push payload from `push-service/` — which is CONTENT-FREE
* ({ title, body, wrapId, url }) because the server never decrypts anything —
* and shows an OS notification. Tapping it focuses/opens the app, where the real
* message is fetched by wrapId and decrypted locally.
*
* Deliberately tiny: no caching, no offline logic.
*/
self.addEventListener('push', (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch {
/* non-JSON payload — fall back to defaults */
}
const title = data.title || 'SkateSpots';
const body = data.body || 'You have a new notification';
event.waitUntil(
self.registration.showNotification(title, {
body,
tag: data.wrapId || undefined,
data: { url: data.url || '/' },
}),
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const target = (event.notification.data && event.notification.data.url) || '/';
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
for (const client of list) {
if ('focus' in client) return client.focus();
}
return self.clients.openWindow(target);
}),
);
});