-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added portable SharedArrayBuffer workaround to single-sample componen…
…t head
- Loading branch information
1 parent
cf47b67
commit 1fe7460
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
ProjectHeads/SingleComponent/Wasm/wwwroot/SharedArrayBufferServiceWorker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// From https://dev.to/stefnotch/enabling-coop-coep-without-touching-the-server-2d3n | ||
|
||
self.addEventListener("install", function () { | ||
self.skipWaiting(); | ||
}); | ||
|
||
self.addEventListener("activate", (event) => { | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
self.addEventListener("fetch", function (event) { | ||
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") { | ||
return; | ||
} | ||
|
||
event.respondWith( | ||
fetch(event.request) | ||
.then(function (response) { | ||
// It seems like we only need to set the headers for index.html | ||
// If you want to be on the safe side, comment this out | ||
// if (!response.url.includes("index.html")) return response; | ||
|
||
const newHeaders = new Headers(response.headers); | ||
newHeaders.set("Cross-Origin-Embedder-Policy", "credentialless"); | ||
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin"); | ||
|
||
const moddedResponse = new Response(response.body, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: newHeaders, | ||
}); | ||
|
||
return moddedResponse; | ||
}) | ||
.catch(function (e) { | ||
console.error(e); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> | ||
|
||
<!-- | ||
Only needed if multithreading is enabled. | ||
Required for using SharedArrayBuffer | ||
--> | ||
<script type="text/javascript" src="scripts/EnableSharedArrayBuffer.js"></script> | ||
<script type="text/javascript" src="./require.js"></script> | ||
<script type="module" src="./uno-bootstrap.js"></script> | ||
$(ADDITIONAL_CSS) | ||
$(ADDITIONAL_HEAD) | ||
</head> | ||
<body> | ||
<div id="uno-body" class="container-fluid uno-body"> | ||
<div class="uno-loader" | ||
loading-position="bottom" | ||
loading-alert="none"> | ||
|
||
<!-- Logo: change src to customize the logo --> | ||
<img class="logo" | ||
src="" | ||
title="Uno is loading your application" /> | ||
|
||
<progress></progress> | ||
<span class="alert"></span> | ||
</div> | ||
</div> | ||
<noscript> | ||
<p>This application requires Javascript and WebAssembly to be enabled.</p> | ||
</noscript> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
ProjectHeads/SingleComponent/Wasm/wwwroot/scripts/EnableSharedArrayBuffer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// From https://dev.to/stefnotch/enabling-coop-coep-without-touching-the-server-2d3n | ||
|
||
if ("serviceWorker" in navigator) { | ||
// Register service worker | ||
navigator.serviceWorker.register(new URL("SharedArrayBufferServiceWorker.js", window.location.href)).then( | ||
function (registration) { | ||
console.log("COOP/COEP Service Worker registered", registration.scope); | ||
// If the registration is active, but it's not controlling the page | ||
if (registration.active && !navigator.serviceWorker.controller) { | ||
window.location.reload(); | ||
} | ||
}, | ||
function (err) { | ||
console.log("COOP/COEP Service Worker failed to register", err); | ||
} | ||
); | ||
} else { | ||
console.warn("Cannot register a service worker"); | ||
} |