shellphone.app/app/entry.worker.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

/// <reference lib="WebWorker" />
2022-06-01 21:56:37 +00:00
import handleInstall from "./service-worker/install";
import handleActivate from "./service-worker/activate";
import handlePush from "./service-worker/push";
import handleNotificationClick from "./service-worker/notification-click";
import handleFetch from "./service-worker/fetch";
2022-06-05 22:01:33 +00:00
import handleMessage from "./service-worker/message";
declare const self: ServiceWorkerGlobalScope;
2022-06-01 21:56:37 +00:00
self.addEventListener("install", (event) => {
event.waitUntil(handleInstall(event).then(() => self.skipWaiting()));
});
self.addEventListener("activate", (event) => {
event.waitUntil(handleActivate(event).then(() => self.clients.claim()));
});
self.addEventListener("push", (event) => {
2022-06-01 21:56:37 +00:00
event.waitUntil(handlePush(event));
});
self.addEventListener("notificationclick", (event) => {
2022-06-01 21:56:37 +00:00
event.waitUntil(handleNotificationClick(event));
});
2022-06-05 22:01:33 +00:00
self.addEventListener("message", (event) => {
event.waitUntil(handleMessage(event));
});
2022-06-01 21:56:37 +00:00
self.addEventListener("fetch", (event) => {
2022-06-27 12:44:52 +00:00
const url = new URL(event.request.url);
const isSSERequest = event.request.headers.get("Accept") === "text/event-stream";
2022-06-27 12:44:52 +00:00
const isOutsideRequest = !["localhost", "dev.shellphone.app", "www.shellphone.app"].includes(url.hostname);
if (isSSERequest || isOutsideRequest) {
return;
}
2022-06-01 21:56:37 +00:00
event.respondWith(handleFetch(event));
});