Spaces:
Running
Running
| /* ====================================================== | |
| VIDRAFT Service Worker β Web Push Notifications v4 | |
| v3μμ .catch() μ²΄μΈ μ κ±° (λͺ¨λ°μΌ silent-fail μμΈ) | |
| Windows νΈν: actions μ κ±° μ μ§ | |
| ====================================================== */ | |
| const CACHE_VERSION = 'vidraft-sw-v4'; | |
| self.addEventListener('install', () => self.skipWaiting()); | |
| self.addEventListener('activate', e => e.waitUntil(self.clients.claim())); | |
| // ββ Push μμ β μλ¦Ό νμ ββββββββββββββββββββββββββββ | |
| self.addEventListener('push', (e) => { | |
| let d = { | |
| title: 'VIDRAFT', | |
| body: 'μ μμμ΄ μμ΅λλ€.', | |
| icon: 'https://vidraft-ai.static.hf.space/favicon.png', | |
| badge: 'https://vidraft-ai.static.hf.space/favicon.png', | |
| image: '', | |
| url: 'https://vidraft.net' | |
| }; | |
| if (e.data) { | |
| try { d = { ...d, ...e.data.json() }; } | |
| catch { d.body = e.data.text(); } | |
| } | |
| console.log('[SW v4] push received, title:', d.title); | |
| const options = { | |
| body: d.body, | |
| icon: d.icon, | |
| badge: d.badge, | |
| tag: 'vidraft-push', | |
| renotify: true, | |
| requireInteraction: false, | |
| data: { url: d.url }, | |
| // actions μ κ±° β Windows Chrome silent-fail μ λ° | |
| }; | |
| if (d.image && d.image.trim()) { | |
| options.image = d.image.trim(); | |
| } | |
| // .catch() μ²΄μΈ μμ΄ μ§μ waitUntil β κ°μ₯ μμ μ | |
| e.waitUntil(self.registration.showNotification(d.title, options)); | |
| }); | |
| // ββ μλ¦Ό ν΄λ¦ β ν μ΄κΈ° βββββββββββββββββββββββββββββ | |
| self.addEventListener('notificationclick', (e) => { | |
| e.notification.close(); | |
| const target = (e.notification.data && e.notification.data.url) | |
| ? e.notification.data.url | |
| : 'https://vidraft.net'; | |
| e.waitUntil( | |
| self.clients.matchAll({ type: 'window', includeUncontrolled: true }) | |
| .then(clients => { | |
| for (const c of clients) { | |
| if (c.url === target && 'focus' in c) return c.focus(); | |
| } | |
| if (self.clients.openWindow) return self.clients.openWindow(target); | |
| }) | |
| ); | |
| }); | |