← Назад
import { precacheAndRoute } from 'workbox-precaching'; // Required by vite-plugin-pwa injectManifest precacheAndRoute(self.__WB_MANIFEST || []); // Immediately skip waiting and activate the new service worker self.addEventListener('install', () => { self.skipWaiting(); }); self.addEventListener('activate', (event) => { event.waitUntil(self.clients.claim()); }); self.addEventListener('push', (event) => { let data = { title: 'Сигнал', body: 'Новое уведомление', icon: '/pwa-192x192.png' }; if (event.data) { try { data = event.data.json(); } catch (e) { data.body = event.data.text(); } } const options = { body: data.body, icon: data.icon || '/pwa-192x192.png', badge: '/pwa-192x192.png', vibrate: [200, 100, 200, 100, 200], data: data.data || {} }; event.waitUntil( self.registration.showNotification(data.title, options) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); // Extract the deep link URL sent from the backend, default to root const urlToOpen = new URL(event.notification.data?.url || '/', self.location.origin).href; event.waitUntil( self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { for (const client of clientList) { // If a window is already open, navigate it to the deep link and focus it if (client.url && 'focus' in client) { client.navigate(urlToOpen); return client.focus(); } } if (self.clients.openWindow) { return self.clients.openWindow(urlToOpen); } }) ); });