|
| 1 | +// NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/) which allows us to use wasm threads. |
| 2 | +// Normally you would set the COOP and COEP headers on the server to do this, but Github Pages doesn't allow this, so this is a hack to do that. |
| 3 | + |
| 4 | +/* Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */ |
| 5 | +// From here: https://github.com/gzuidhof/coi-serviceworker |
| 6 | +if (typeof window === "undefined") { |
| 7 | + self.addEventListener("install", () => self.skipWaiting()) |
| 8 | + self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim())) |
| 9 | + |
| 10 | + async function handleFetch(request) { |
| 11 | + if (request.cache === "only-if-cached" && request.mode !== "same-origin") { |
| 12 | + return |
| 13 | + } |
| 14 | + |
| 15 | + if (request.mode === "no-cors") { |
| 16 | + // We need to set `credentials` to "omit" for no-cors requests, per this comment: https://bugs.chromium.org/p/chromium/issues/detail?id=1309901#c7 |
| 17 | + request = new Request(request.url, { |
| 18 | + cache: request.cache, |
| 19 | + credentials: "omit", |
| 20 | + headers: request.headers, |
| 21 | + integrity: request.integrity, |
| 22 | + destination: request.destination, |
| 23 | + keepalive: request.keepalive, |
| 24 | + method: request.method, |
| 25 | + mode: request.mode, |
| 26 | + redirect: request.redirect, |
| 27 | + referrer: request.referrer, |
| 28 | + referrerPolicy: request.referrerPolicy, |
| 29 | + signal: request.signal, |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + const r = await fetch(request).catch((e) => console.error(e)) |
| 34 | + |
| 35 | + if (r.status === 0) { |
| 36 | + return r |
| 37 | + } |
| 38 | + |
| 39 | + const headers = new Headers(r.headers) |
| 40 | + headers.set("Cross-Origin-Embedder-Policy", "credentialless") // or: require-corp |
| 41 | + headers.set("Cross-Origin-Opener-Policy", "same-origin") |
| 42 | + |
| 43 | + return new Response(r.body, { |
| 44 | + status: r.status, |
| 45 | + statusText: r.statusText, |
| 46 | + headers, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + self.addEventListener("fetch", (e) => { |
| 51 | + e.respondWith(handleFetch(e.request)) // respondWith must be executed synchonously (but can be passed a Promise) |
| 52 | + }) |
| 53 | +} else { |
| 54 | + ;(async () => { |
| 55 | + if (window.crossOriginIsolated !== false) return |
| 56 | + |
| 57 | + const registration = await navigator.serviceWorker |
| 58 | + .register(window.document.currentScript.src) |
| 59 | + .catch((e) => |
| 60 | + console.error("COOP/COEP Service Worker failed to register:", e), |
| 61 | + ) |
| 62 | + if (registration) { |
| 63 | + console.log("COOP/COEP Service Worker registered", registration.scope) |
| 64 | + |
| 65 | + registration.addEventListener("updatefound", () => { |
| 66 | + console.log( |
| 67 | + "Reloading page to make use of updated COOP/COEP Service Worker.", |
| 68 | + ) |
| 69 | + window.location.reload() |
| 70 | + }) |
| 71 | + |
| 72 | + // If the registration is active, but it's not controlling the page |
| 73 | + if (registration.active && !navigator.serviceWorker.controller) { |
| 74 | + console.log("Reloading page to make use of COOP/COEP Service Worker.") |
| 75 | + window.location.reload() |
| 76 | + } |
| 77 | + } |
| 78 | + })() |
| 79 | +} |
| 80 | + |
| 81 | +// Code to deregister: |
| 82 | +// let registrations = await navigator.serviceWorker.getRegistrations(); |
| 83 | +// for(let registration of registrations) { |
| 84 | +// await registration.unregister(); |
| 85 | +// } |
0 commit comments