Skip to content

Commit

Permalink
Added portable SharedArrayBuffer workaround to single-sample componen…
Browse files Browse the repository at this point in the history
…t head
  • Loading branch information
Arlodotexe committed Aug 2, 2023
1 parent cf47b67 commit 1fe7460
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
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);
})
);
});
38 changes: 38 additions & 0 deletions ProjectHeads/SingleComponent/Wasm/wwwroot/index.html
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>
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");
}

0 comments on commit 1fe7460

Please sign in to comment.