Web Push (VAPID) backend: keys auto-generated on first run, per-user subscriptions, one new dependency (web-push). The rest timer schedules a server-side push on start/extend and cancels it on skip or on-screen completion, so it only actually fires if the tab got suspended before finishing. A per-minute check sends an optional daily reminder on days with a planned routine that hasn't been logged yet. Opt in per-profile in Settings — requires a signed-in passkey profile.
46 lines
1.7 KiB
JavaScript
46 lines
1.7 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()))
|
|
})
|
|
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
|
|
}))
|
|
})
|
|
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('./')
|
|
}))
|
|
})
|
|
|
|
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'))))
|
|
}
|
|
})
|