feat: add webchat pwa push subscription support
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
const CACHE_NAME = 'flynn-webchat-v1';
|
||||
const token = new URL(self.location.href).searchParams.get('token');
|
||||
const withToken = (path) => {
|
||||
if (!token) {
|
||||
return path;
|
||||
}
|
||||
const sep = path.includes('?') ? '&' : '?';
|
||||
return `${path}${sep}token=${encodeURIComponent(token)}`;
|
||||
};
|
||||
const OFFLINE_ASSETS = ['/', '/index.html', '/style.css', '/app.js', '/manifest.webmanifest'].map(withToken);
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then((cache) => cache.addAll(OFFLINE_ASSETS))
|
||||
.catch(() => undefined),
|
||||
);
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((keys) => Promise.all(
|
||||
keys
|
||||
.filter((key) => key !== CACHE_NAME)
|
||||
.map((key) => caches.delete(key)),
|
||||
)),
|
||||
);
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
if (event.request.method !== 'GET') {
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith((async () => {
|
||||
const cached = await caches.match(event.request, { ignoreSearch: true });
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
try {
|
||||
return await fetch(event.request);
|
||||
} catch {
|
||||
const fallback = await caches.match('/index.html', { ignoreSearch: true });
|
||||
return fallback || new Response('Offline', { status: 503 });
|
||||
}
|
||||
})());
|
||||
});
|
||||
|
||||
self.addEventListener('push', (event) => {
|
||||
let payload = {
|
||||
title: 'Flynn',
|
||||
body: 'You have a new update.',
|
||||
};
|
||||
|
||||
try {
|
||||
const parsed = event.data?.json();
|
||||
if (parsed && typeof parsed === 'object') {
|
||||
payload = {
|
||||
...payload,
|
||||
...parsed,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
const text = event.data?.text();
|
||||
if (text) {
|
||||
payload.body = text;
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(self.registration.showNotification(payload.title, {
|
||||
body: payload.body,
|
||||
tag: payload.tag || 'flynn-webchat',
|
||||
renotify: false,
|
||||
data: payload.data || {},
|
||||
}));
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
event.notification.close();
|
||||
|
||||
event.waitUntil((async () => {
|
||||
const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
if (clients.length > 0) {
|
||||
await clients[0].focus();
|
||||
return;
|
||||
}
|
||||
await self.clients.openWindow('/#/chat');
|
||||
})());
|
||||
});
|
||||
Reference in New Issue
Block a user