Skip to content

Commit 768f129

Browse files
committed
本番環境でのエラーのホットフィックス
1 parent 15161c9 commit 768f129

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Python Editor</title>
8+
<script src="/src/enable-threads.js" defer></script>
89
</head>
910
<body>
1011
<h1>Python Editor</h1>
@@ -31,7 +32,7 @@ <h2 id="input-label">Input</h2>
3132
</div>
3233

3334
<h2 id="state-label">State</h2>
34-
<p id="state" aria-labelledby="state-label">None</p>
35+
<p id="state">None</p>
3536

3637
<h2 id="output-label">Output</h2>
3738
<textarea

src/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import "ace-builds/src-noconflict/mode-python"
33
import "ace-builds/src-noconflict/theme-github"
44
import "ace-builds/src-noconflict/theme-github_dark"
55
import "ace-builds/src-noconflict/keybinding-vscode"
6+
import "ace-builds/src-noconflict/ext-language_tools"
67
import template_python_code from "./main.py?raw"
78

89
const media = window.matchMedia("(prefers-color-scheme: dark)")

src/enable-threads.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)