awacke1 commited on
Commit
20de9af
β€’
1 Parent(s): 26360ff

Update enable-threads.js

Browse files
Files changed (1) hide show
  1. 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/) 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",
@@ -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
- 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
  }
@@ -68,7 +87,7 @@ if(typeof window === 'undefined') {
68
  })();
69
  }
70
 
71
- // Code to deregister:
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();