Build & Deploy / build (push) Successful in 6s
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
/// <reference lib="webworker" />
|
|
import { precacheAndRoute } from 'workbox-precaching'
|
|
|
|
declare const self: ServiceWorkerGlobalScope & { __WB_MANIFEST: Array<{ url: string, revision: string | null }> }
|
|
|
|
// Précache des assets de l'app (injecté au build).
|
|
precacheAndRoute(self.__WB_MANIFEST)
|
|
|
|
self.skipWaiting()
|
|
self.addEventListener('activate', event => event.waitUntil(self.clients.claim()))
|
|
|
|
// Réception d'un push : affiche la notification système (le contenu vient du back).
|
|
self.addEventListener('push', (event: PushEvent) => {
|
|
let payload: { title?: string, body?: string, icon?: string, badge?: string, data?: { url?: string } } = {}
|
|
try {
|
|
payload = event.data?.json() ?? {}
|
|
} catch {
|
|
payload = { body: event.data?.text() }
|
|
}
|
|
|
|
const title = payload.title || 'StreakFit'
|
|
event.waitUntil(
|
|
self.registration.showNotification(title, {
|
|
body: payload.body,
|
|
icon: payload.icon || '/pwa-192.png',
|
|
badge: payload.badge || '/pwa-192.png',
|
|
data: payload.data || { url: '/' },
|
|
tag: 'streakfit-streak',
|
|
renotify: true
|
|
})
|
|
)
|
|
})
|
|
|
|
// Clic sur la notification : ouvre/focus l'app sur l'URL fournie.
|
|
self.addEventListener('notificationclick', (event: NotificationEvent) => {
|
|
event.notification.close()
|
|
const url = (event.notification.data && event.notification.data.url) || '/'
|
|
event.waitUntil(
|
|
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
|
|
for (const client of clients) {
|
|
if ('focus' in client) return client.focus()
|
|
}
|
|
return self.clients.openWindow(url)
|
|
})
|
|
)
|
|
})
|