Push: reminders owed within 15 minutes still arrive, the subscription heals itself on every boot, rest-timer alerts belong to the device that started the rest
- reminder tick: a due reminder is sent for up to 15 minutes after its time (once per local date) instead of only on the exact minute — a restart across that minute used to lose the day; state files are stat'ed and re-read only when they changed - sendPush prunes 403 (stale VAPID key) like 404/410; optional device targeting - POST /api/push/subscribe is an upsert that keeps created and stores a deviceId; GET /api/push/status says whether the server holds an endpoint - rest timers keyed per user+device; cancel without a device id keeps the old account-wide behaviour - sw.js: payload parsed inside waitUntil (never a silent push), pushsubscriptionchange re-subscribes and registers the new endpoint - lib/push.js: syncPushSubscription() (called once per signed-in boot from App.jsx and when Settings opens) re-registers a lost row, replaces a subscription made against another key, pushSupported() is false off https; deviceId() per browser - Settings: the notifications switch shows what the server holds - tests: server-reminder (window, dedupe, trained day), server-push-status (upsert, status, per-device timers), lib/push.test.js
This commit is contained in:
1 parent
c92cfc372a
commit
477fc07f8b
10 files changed
+534
-44
No files matched your search
+39
-8
@@ -8,15 +8,21 @@ self.addEventListener('activate', e => {
|
||||
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
||||
).then(() => self.clients.claim()))
|
||||
})
|
||||
// Every push shows a notification, whatever the payload: a push that shows nothing counts
|
||||
// against the site (Chrome revokes the subscription after a few), and a payload that fails to
|
||||
// parse used to throw before waitUntil was ever reached — exactly such a silent push.
|
||||
self.addEventListener('push', e => {
|
||||
const data = e.data ? e.data.json() : {}
|
||||
e.waitUntil(self.registration.showNotification(data.title || 'openGym', {
|
||||
body: data.body || '',
|
||||
icon: 'icon-512.png',
|
||||
badge: 'icon-180.png',
|
||||
tag: data.tag || 'opengym',
|
||||
renotify: true
|
||||
}))
|
||||
e.waitUntil((async () => {
|
||||
let data = {}
|
||||
try { data = e.data ? e.data.json() : {} } catch { data = { body: (() => { try { return e.data.text() } catch { return '' } })() } }
|
||||
await self.registration.showNotification(data.title || 'openGym', {
|
||||
body: data.body || '',
|
||||
icon: 'icon-512.png',
|
||||
badge: 'icon-180.png',
|
||||
tag: data.tag || 'opengym',
|
||||
renotify: true
|
||||
})
|
||||
})())
|
||||
})
|
||||
self.addEventListener('notificationclick', e => {
|
||||
e.notification.close()
|
||||
@@ -26,6 +32,31 @@ self.addEventListener('notificationclick', e => {
|
||||
}))
|
||||
})
|
||||
|
||||
// The push service rotated the subscription (it does, unannounced, every few months on some
|
||||
// platforms). Without this the server keeps sending to the old endpoint until it 410s, and the
|
||||
// browser holds a new one nobody registered — "notifications just stopped". Re-subscribe with
|
||||
// the same application server key and hand the new endpoint to the API; the session cookie
|
||||
// travels with the same-origin fetch. Signed out, the API answers 401 and the next signed-in
|
||||
// boot (lib/push.js syncPushSubscription) registers it instead.
|
||||
const b64ToBytes = b64 => {
|
||||
const padded = (b64 + '='.repeat((4 - b64.length % 4) % 4)).replace(/-/g, '+').replace(/_/g, '/')
|
||||
return Uint8Array.from(atob(padded), c => c.charCodeAt(0))
|
||||
}
|
||||
self.addEventListener('pushsubscriptionchange', e => {
|
||||
e.waitUntil((async () => {
|
||||
let key = e.oldSubscription?.options?.applicationServerKey
|
||||
if (!key) {
|
||||
const r = await fetch(new URL('api/push/public-key', self.registration.scope))
|
||||
key = b64ToBytes((await r.json()).key)
|
||||
}
|
||||
const sub = e.newSubscription || await self.registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: key })
|
||||
await fetch(new URL('api/push/subscribe', self.registration.scope), {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ subscription: sub.toJSON() })
|
||||
})
|
||||
})().catch(() => {}))
|
||||
})
|
||||
|
||||
self.addEventListener('fetch', e => {
|
||||
const url = new URL(e.request.url)
|
||||
if (e.request.method !== 'GET' || url.origin !== location.origin) return
|
||||
|
||||
Reference in new issue
Block a user