diff --git a/public/sw.js b/public/sw.js new file mode 100644 index 0000000..f696d32 --- /dev/null +++ b/public/sw.js @@ -0,0 +1,38 @@ +self.addEventListener('push', function (event) { + let data = {}; + + try { + data = event.data?.json() ?? {}; + } catch (e) { + data = { title: 'Notification', body: event.data?.text() ?? '' }; + } + + event.waitUntil( + self.registration.showNotification(data.title, { + body: data.body, + icon: data.icon, + badge: data.badge, + data: data.data, + actions: data.actions, + }), + ); +}); + +self.addEventListener('notificationclick', function (event) { + event.notification.close(); + + const url = event.notification.data?.url ?? '/'; + + event.waitUntil( + self.clients + .matchAll({ type: 'window', includeUncontrolled: true }) + .then(function (clientList) { + for (const client of clientList) { + if (client.url === url && 'focus' in client) { + return client.focus(); + } + } + return self.clients.openWindow(url); + }), + ); +}); diff --git a/vite.config.ts b/vite.config.ts index d830a40..9be3e29 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -35,27 +35,27 @@ export default defineConfig({ wayfinder({ formVariants: true, }), - VitePWA({ - registerType: 'autoUpdate', - injectRegister: 'auto', - workbox: { - globPatterns: ['**/*.{js,css,html,ico,png,svg}'], - runtimeCaching: [ - { - urlPattern: ({ url }) => url.pathname.startsWith('/'), - handler: 'NetworkFirst', - options: { - cacheName: 'api-cache', - cacheableResponse: { - statuses: [0, 200], - }, - }, - }, - ], - }, - srcDir: 'resources/js', - filename: 'sw.js', - outDir: 'public', - }), + // VitePWA({ + // registerType: 'autoUpdate', + // injectRegister: 'auto', + // workbox: { + // globPatterns: ['**/*.{js,css,html,ico,png,svg}'], + // runtimeCaching: [ + // { + // urlPattern: ({ url }) => url.pathname.startsWith('/'), + // handler: 'NetworkFirst', + // options: { + // cacheName: 'api-cache', + // cacheableResponse: { + // statuses: [0, 200], + // }, + // }, + // }, + // ], + // }, + // srcDir: 'resources/js', + // filename: 'sw.js', + // outDir: 'public', + // }), ], });