Files
kuentagym/frontend/public/sw.js
T
Duarte Santos 477fc07f8b 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
2026-09-12 13:18:29 +02:00

77 lines
3.5 KiB
JavaScript

/* openGym service worker — runtime caching (works with Vite's hashed asset names).
Media (img/gif) cache-first; everything else network-first with offline fallback. */
const CACHE = 'opengym-rt-v1'
self.addEventListener('install', () => self.skipWaiting())
self.addEventListener('activate', e => {
e.waitUntil(caches.keys().then(keys =>
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 => {
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()
e.waitUntil(self.clients.matchAll({ type: 'window' }).then(clients => {
const c = clients.find(c => 'focus' in c)
return c ? c.focus() : self.clients.openWindow('./')
}))
})
// 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
if (url.pathname.startsWith('/api/')) return // never cache auth/data
const isMedia = url.pathname.includes('/img/') || url.pathname.includes('/gif/')
if (isMedia) {
e.respondWith(caches.open(CACHE).then(c => c.match(e.request).then(hit =>
hit || fetch(e.request).then(res => { if (res.ok) c.put(e.request, res.clone()); return res })
)))
} else {
e.respondWith(fetch(e.request).then(res => {
if (res.ok) caches.open(CACHE).then(c => c.put(e.request, res.clone()))
return res
}).catch(() => caches.match(e.request).then(hit => hit || caches.match('index.html'))))
}
})