2021-06-22 09:22:35 -04:00
|
|
|
/// <reference lib="webworker" />
|
2021-07-05 06:23:23 -04:00
|
|
|
import { precacheAndRoute } from "workbox-precaching";
|
2021-06-22 09:22:35 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
declare let self: ServiceWorkerGlobalScope;
|
2021-06-22 09:22:35 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
self.addEventListener("message", (event) => {
|
2021-07-05 06:25:20 -04:00
|
|
|
if (event.data && event.data.type === "SKIP_WAITING") self.skipWaiting();
|
2021-07-05 06:23:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST);
|
2021-06-22 09:37:38 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
self.addEventListener("push", (event) => {
|
2021-07-05 06:25:20 -04:00
|
|
|
async function process() {
|
2021-09-02 17:24:22 -04:00
|
|
|
if (event.data) {
|
|
|
|
const data = event.data.json();
|
|
|
|
await self.registration.showNotification(data.author, {
|
|
|
|
icon: data.icon,
|
|
|
|
image: data.image,
|
|
|
|
body: data.body,
|
2021-12-20 08:37:21 -05:00
|
|
|
timestamp: data.timestamp * 1000,
|
2021-09-02 17:24:22 -04:00
|
|
|
tag: data.tag,
|
2021-12-20 08:37:21 -05:00
|
|
|
badge: "https://app.revolt.chat/assets/icons/monochrome.svg",
|
2021-11-20 07:53:35 -05:00
|
|
|
data: data.url,
|
2021-09-02 17:24:22 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 06:25:20 -04:00
|
|
|
event.waitUntil(process());
|
2021-06-22 09:37:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// ? Open the app on notification click.
|
|
|
|
// https://stackoverflow.com/a/39457287
|
2021-07-10 10:57:29 -04:00
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
|
|
const url = event.notification.data;
|
2021-07-05 06:25:20 -04:00
|
|
|
event.notification.close();
|
|
|
|
event.waitUntil(
|
|
|
|
self.clients
|
|
|
|
.matchAll({ includeUncontrolled: true, type: "window" })
|
|
|
|
.then((windowClients) => {
|
|
|
|
// Check if there is already a window/tab open with the target URL
|
2021-07-10 10:57:29 -04:00
|
|
|
for (let i = 0; i < windowClients.length; i++) {
|
|
|
|
const client = windowClients[i];
|
2021-07-05 06:25:20 -04:00
|
|
|
// If so, just focus it.
|
|
|
|
if (client.url === url && "focus" in client) {
|
|
|
|
return client.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, then open the target URL in a new window/tab.
|
|
|
|
if (self.clients.openWindow) {
|
|
|
|
return self.clients.openWindow(url);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2021-06-22 09:37:38 -04:00
|
|
|
});
|