Skip to content

Commit

Permalink
Switch from self to global for service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Aug 2, 2024
1 parent 380ab17 commit 3080c97
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/serviceworker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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") {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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" });
Expand Down

0 comments on commit 3080c97

Please sign in to comment.