From 3080c97007cf3f971138cf2b985a252985a4fbd6 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 1 Aug 2024 19:46:03 -0600 Subject: [PATCH] Switch from `self` to `global` for service worker --- src/serviceworker/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/serviceworker/index.ts b/src/serviceworker/index.ts index 736b536613..fc3e04fd51 100644 --- a/src/serviceworker/index.ts +++ b/src/serviceworker/index.ts @@ -25,13 +25,13 @@ const serverSupportMap: { }; } = {}; -self.addEventListener("install", (event) => { +global.addEventListener("install", (event) => { // We skipWaiting() to update the service worker more frequently, particularly in development environments. // @ts-expect-error - service worker types are not available. See 'fetch' event handler. event.waitUntil(skipWaiting()); }); -self.addEventListener("activate", (event) => { +global.addEventListener("activate", (event) => { // We force all clients to be under our control, immediately. This could be old tabs. // @ts-expect-error - service worker types are not available. See 'fetch' event handler. event.waitUntil(clients.claim()); @@ -40,7 +40,7 @@ self.addEventListener("activate", (event) => { // @ts-expect-error - the service worker types conflict with the DOM types available through TypeScript. Many hours // have been spent trying to convince the type system that there's no actual conflict, but it has yet to work. Instead // of trying to make it do the thing, we force-cast to something close enough where we can (and ignore errors otherwise). -self.addEventListener("fetch", (event: FetchEvent) => { +global.addEventListener("fetch", (event: FetchEvent) => { // This is the authenticated media (MSC3916) check, proxying what was unauthenticated to the authenticated variants. if (event.request.method !== "GET") { @@ -72,7 +72,7 @@ self.addEventListener("fetch", (event: FetchEvent) => { // Locate our access token, and populate the fetchConfig with the authentication header. // @ts-expect-error - service worker types are not available. See 'fetch' event handler. - const client = await self.clients.get(event.clientId); + const client = await global.clients.get(event.clientId); accessToken = await getAccessToken(client); // Update or populate the server support map using a (usually) authenticated `/versions` call. @@ -166,9 +166,9 @@ async function askClientForUserIdParams(client: unknown): Promise<{ userId: stri if (event.data?.responseKey !== responseKey) return; // not for us clearTimeout(timeoutId); // do this as soon as possible, avoiding a race between resolve and reject. resolve(event.data); // "unblock" the remainder of the thread, if that were such a thing in JavaScript. - self.removeEventListener("message", listener); // cleanup, since we're not going to do anything else. + global.removeEventListener("message", listener); // cleanup, since we're not going to do anything else. }; - self.addEventListener("message", listener); + global.addEventListener("message", listener); // Ask the tab for the information we need. This is handled by WebPlatform. (client as Window).postMessage({ responseKey, type: "userinfo" });