Skip to content

Commit 480b691

Browse files
committed
Add COI hack for GitHub pages
1 parent 26ad19d commit 480b691

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

enable-threads.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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") { // 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
16+
request = new Request(request.url, {
17+
cache: request.cache,
18+
credentials: "omit",
19+
headers: request.headers,
20+
integrity: request.integrity,
21+
destination: request.destination,
22+
keepalive: request.keepalive,
23+
method: request.method,
24+
mode: request.mode,
25+
redirect: request.redirect,
26+
referrer: request.referrer,
27+
referrerPolicy: request.referrerPolicy,
28+
signal: request.signal,
29+
});
30+
}
31+
32+
let r = await fetch(request).catch(e => console.error(e));
33+
34+
if(r.status === 0) {
35+
return r;
36+
}
37+
38+
const headers = new Headers(r.headers);
39+
headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // or: require-corp
40+
headers.set("Cross-Origin-Opener-Policy", "same-origin");
41+
42+
return new Response(r.body, { status: r.status, statusText: r.statusText, headers });
43+
}
44+
45+
self.addEventListener("fetch", function(e) {
46+
e.respondWith(handleFetch(e.request)); // respondWith must be executed synchonously (but can be passed a Promise)
47+
});
48+
49+
} else {
50+
(async function() {
51+
if(window.crossOriginIsolated !== false) return;
52+
53+
let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));
54+
if(registration) {
55+
console.log("COOP/COEP Service Worker registered", registration.scope);
56+
57+
registration.addEventListener("updatefound", () => {
58+
console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
59+
window.location.reload();
60+
});
61+
62+
// If the registration is active, but it's not controlling the page
63+
if(registration.active && !navigator.serviceWorker.controller) {
64+
console.log("Reloading page to make use of COOP/COEP Service Worker.");
65+
window.location.reload();
66+
}
67+
}
68+
})();
69+
}
70+
71+
// Code to deregister:
72+
// let registrations = await navigator.serviceWorker.getRegistrations();
73+
// for(let registration of registrations) {
74+
// await registration.unregister();
75+
// }

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
src: url(fonts/acme_7_wide_xtnd.woff);
5959
}
6060
</style>
61+
<!-- Hack for GitHub Pages found on https://github.com/orgs/community/discussions/13309#discussioncomment-3844940 -->
62+
<script src="enable-threads.js"></script>
6163
<script src="raylib-wrapper.js"></script>
6264
</head>
6365
<body>

0 commit comments

Comments
 (0)