3
  • Hi, Chrome has implemented some requirements to use SharedArrayBuffer, we have to include two headers:

Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp

  • Is there any way that we can use SharedArrayBuffer on Github? I have try netlify but they have a limit on bandwidth. Is there any hosting static site similar GitHub that can allow us custom request header so we can use SharedArrayBuffer?
Đức Huỳnh
  • 97
  • 2
  • 12

4 Answers4

2

You can get SharedArrayBuffer to work by setting the required COOP and COEP headers through a service worker (even on Github Pages). This blogpost will explain the basics and show you how.

Essentially, the idea is to

  1. when the page gets loaded for the first time, we register the worker
  2. then we reload the page
  3. and finally, now that the worker is controlling everything, every request will now have the appropriate headers set

The service worker which does that has to contain something along the lines of

// sw.js
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) {
        const newHeaders = new Headers(response.headers);
        newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp");
        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);
      })
  );
});

I created a small library just now to make it easier: coi-serviceworker.

Stefnotch
  • 209
  • 2
  • 10
  • 20
gzuidhof
  • 57
  • 5
1

As of August 2021 there's no way for GitHub pages to serve with COOP/COEP headers. As an alternative static file server with custom headers, Firebase hosting might be an option. I'm not familiar with other options.

agektmr
  • 1,929
  • 13
  • 13
0

Look likes Github don't have the intent to add custom headers, found the same question in 2011 and until now still don't have this Github pages, HTTP headers

Đức Huỳnh
  • 97
  • 2
  • 12
0

I have try netlify but they have a limit on bandwidth.

I use Cloudflare Workers to add such headers. They won't limit you in terms of bandwidth (which is good for hefty WebAssembly files), but they do have a limit for number of requests on the free plan - 10,000,000 requests per month. https://developers.cloudflare.com/workers/platform/pricing

To be fair though, that limit is usually hard to hit on small demos (which is what Github Pages is usually for), and after that the price is not very steep either ($0.50 for each extra 1M requests).

RReverser
  • 1,744
  • 13
  • 15