Switch from `self` to `global` for service worker

pull/27891/head
Travis Ralston 2024-08-01 19:46:03 -06:00
parent 380ab17932
commit 3080c97007
1 changed files with 6 additions and 6 deletions

View File

@ -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" });