Spaces:
Running
Running
Update enable-threads.js
Browse files- enable-threads.js +40 -21
enable-threads.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
| 1 |
-
// NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/)
|
| 2 |
-
// Normally you
|
|
|
|
| 3 |
|
| 4 |
-
/* Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */
|
| 5 |
-
//
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
self.addEventListener("install", () => self.skipWaiting());
|
|
|
|
|
|
|
| 8 |
self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));
|
| 9 |
|
|
|
|
| 10 |
async function handleFetch(request) {
|
| 11 |
-
|
|
|
|
| 12 |
return;
|
| 13 |
}
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
request = new Request(request.url, {
|
| 17 |
cache: request.cache,
|
| 18 |
credentials: "omit",
|
|
@@ -28,39 +38,48 @@ if(typeof window === 'undefined') {
|
|
| 28 |
signal: request.signal,
|
| 29 |
});
|
| 30 |
}
|
| 31 |
-
|
|
|
|
| 32 |
let r = await fetch(request).catch(e => console.error(e));
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
return r;
|
| 36 |
}
|
| 37 |
|
|
|
|
| 38 |
const headers = new Headers(r.headers);
|
| 39 |
-
headers.set("Cross-Origin-Embedder-Policy", "credentialless"); //
|
| 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
|
| 47 |
});
|
| 48 |
-
|
| 49 |
} else {
|
|
|
|
| 50 |
(async function() {
|
| 51 |
-
|
|
|
|
| 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 |
-
|
|
|
|
| 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
|
| 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 |
}
|
|
@@ -68,7 +87,7 @@ if(typeof window === 'undefined') {
|
|
| 68 |
})();
|
| 69 |
}
|
| 70 |
|
| 71 |
-
// Code to
|
| 72 |
// let registrations = await navigator.serviceWorker.getRegistrations();
|
| 73 |
// for(let registration of registrations) {
|
| 74 |
// await registration.unregister();
|
|
|
|
| 1 |
+
// π€ NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/)
|
| 2 |
+
// π Normally you'd set the COOP and COEP headers on the server (yum), but Github Pages said: "No burgers for you!" π
|
| 3 |
+
// π So, we're doing a cheeky pizza hack π to make this work without server-side headers.
|
| 4 |
|
| 5 |
+
/* π οΈ Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT π οΈ */
|
| 6 |
+
// π§ββοΈ We're using wizardry from here: https://github.com/gzuidhof/coi-serviceworker
|
| 7 |
+
|
| 8 |
+
if (typeof window === 'undefined') {
|
| 9 |
+
// π οΈ Service Worker Time! (No windows allowed in this club)
|
| 10 |
+
|
| 11 |
+
// π Install event: "Let's skip the line and go straight to work!"
|
| 12 |
self.addEventListener("install", () => self.skipWaiting());
|
| 13 |
+
|
| 14 |
+
// π Activate event: "I claim all clients! Mine! All mine!" (Mwahaha π)
|
| 15 |
self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));
|
| 16 |
|
| 17 |
+
// π΄ Handle fetch requests, like a master chef in the kitchen!
|
| 18 |
async function handleFetch(request) {
|
| 19 |
+
// π§ Special seasoning: If we're caching but the mode's not right, we pass.
|
| 20 |
+
if (request.cache === "only-if-cached" && request.mode !== "same-origin") {
|
| 21 |
return;
|
| 22 |
}
|
| 23 |
+
|
| 24 |
+
// πΆοΈ For no-cors requests, we're keeping it cool π with 'omit' credentials (Bug workarounds are fun, right? π)
|
| 25 |
+
if (request.mode === "no-cors") {
|
| 26 |
request = new Request(request.url, {
|
| 27 |
cache: request.cache,
|
| 28 |
credentials: "omit",
|
|
|
|
| 38 |
signal: request.signal,
|
| 39 |
});
|
| 40 |
}
|
| 41 |
+
|
| 42 |
+
// π Fetching data like a worldwide explorer! π§
|
| 43 |
let r = await fetch(request).catch(e => console.error(e));
|
| 44 |
+
|
| 45 |
+
// π If the response status is zero, we return it β probably not what we were hoping for, but hey, it's something π€·ββοΈ
|
| 46 |
+
if (r.status === 0) {
|
| 47 |
return r;
|
| 48 |
}
|
| 49 |
|
| 50 |
+
// π© Magic header time! Setting the Cross-Origin rules like a boss π§ββοΈ
|
| 51 |
const headers = new Headers(r.headers);
|
| 52 |
+
headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // π‘οΈ Or: 'require-corp', for those fancy users
|
| 53 |
+
headers.set("Cross-Origin-Opener-Policy", "same-origin"); // π§± Keep it locked down and safe.
|
| 54 |
+
|
| 55 |
return new Response(r.body, { status: r.status, statusText: r.statusText, headers });
|
| 56 |
}
|
| 57 |
|
| 58 |
+
// ποΈ Fetch event listener: "Don't worry, I've got this!" Handling requests like a pro π―
|
| 59 |
self.addEventListener("fetch", function(e) {
|
| 60 |
+
e.respondWith(handleFetch(e.request)); // π‘ respondWith must be synchronous, like a well-timed joke! (But it can wait for a promise π)
|
| 61 |
});
|
| 62 |
+
|
| 63 |
} else {
|
| 64 |
+
// π If we're running in a window (hello, browser!), we register the service worker like a superhero suiting up π¦ΈββοΈ
|
| 65 |
(async function() {
|
| 66 |
+
// β If we're already isolated, let's not double down on the isolation π
|
| 67 |
+
if (window.crossOriginIsolated !== false) return;
|
| 68 |
|
| 69 |
+
// ποΈ Registering the service worker like we're entering the coolest club in town!
|
| 70 |
let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));
|
| 71 |
+
|
| 72 |
+
if (registration) {
|
| 73 |
console.log("COOP/COEP Service Worker registered", registration.scope);
|
| 74 |
|
| 75 |
+
// π When the service worker updates, we refresh the page like a fresh cup of coffee β
|
| 76 |
registration.addEventListener("updatefound", () => {
|
| 77 |
console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
|
| 78 |
window.location.reload();
|
| 79 |
});
|
| 80 |
|
| 81 |
+
// π If the service worker is active but not in control, we give the page a fresh reboot π
|
| 82 |
+
if (registration.active && !navigator.serviceWorker.controller) {
|
| 83 |
console.log("Reloading page to make use of COOP/COEP Service Worker.");
|
| 84 |
window.location.reload();
|
| 85 |
}
|
|
|
|
| 87 |
})();
|
| 88 |
}
|
| 89 |
|
| 90 |
+
// ποΈ Code to clean up: "Time to say goodbye!" Unregister the service worker and take out the trash π§Ή
|
| 91 |
// let registrations = await navigator.serviceWorker.getRegistrations();
|
| 92 |
// for(let registration of registrations) {
|
| 93 |
// await registration.unregister();
|