stefnotch / quantum-sheet

QuantumSheet - A user friendly mathematics worksheet and solver
https://stefnotch.github.io/quantum-sheet/
GNU General Public License v3.0
55 stars 5 forks source link

Use Serviceworker to enable COOP/COEP #16

Open stefnotch opened 3 years ago

stefnotch commented 3 years ago

https://stackoverflow.com/questions/42585254/is-it-possible-to-modify-service-worker-cache-response-headers https://stackoverflow.com/questions/35420980/how-to-alter-the-headers-of-a-request https://w3c-test.org/html/cross-origin-opener-policy/popup-coop-by-sw.https.html

stefnotch commented 3 years ago

Damn, this stupid idea actually works. Though, the minor limitation is that it only works after a page reload. This means that the first time QuantumSheet is visited, it would have to almost instantly reload itself. I guess that's fine until a better solution comes along.

A less stupid solution might be to deploy the page on netlify.


self.addEventListener('fetch', function(event) {
  event.respondWith(fetch(event.request).then(function (response) {    
    const newHeaders = new Headers(response.headers);
    newHeaders.append('Cross-Origin-Embedder-Policy', 'require-corp');
    newHeaders.append('Cross-Origin-Opener-Policy', 'same-origin');

    console.log(JSON.stringify(newHeaders));

    const moddedResponse = new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: newHeaders,
    });

    return moddedResponse;
  }).catch(function (e) {
    console.error(e);
  }));

sw-test-gh-pages.zip

stefnotch commented 3 years ago

Update: Firefox doesn't support service workers in private windows, which is an irritating bug https://bugzilla.mozilla.org/show_bug.cgi?id=1320796

Also, the index.html needs to be the file with the COOP/COEP headers. Adding them to the webworker doesn't cut it?

stefnotch commented 3 years ago

https://github.com/gzuidhof/coi-serviceworker