coi-serviceworker
Cross-origin isolation (COOP and COEP) through a service worker for situations in which you can't control the headers (e.g. GH pages).
Usage
- Download
coi-serviceworker.js
(or coi-serviceworker.min.js
). - Put it next to your index file (or in any folder above it)
- Add to your HTML file:
<script src="coi-serviceworker.js"></script>
This script will reload the page on the user's first load to magically add the required COOP and COEP headers in a service worker.
Rules:
- It must be in a separate file, you can't bundle it along with your app.
- It can't be loaded from a CDN: it must be served from your own origin.
- Your page will still need to be either served from HTTPS, or served from localhost.
You can install this package from npm:
npm i --save coi-serviceworker
You will still have to tell your bundler to put the file alongside your bundle. Something like this will do the trick:
cp node_modules/coi-serviceworker/coi-serviceworker.js dist/
Customization
You can customize the behavior by defining a variable coi
in the global scope (i.e. on the window
object):
window.coi = {
shouldRegister: () => true,
shouldDeregister: () => false,
doReload: () => window.location.reload(),
quiet: false
}
Library and idea based on @stefnotch's blog post.
License
MIT
Carp or Koi (1926) by Ohara Koson. Original from the Los Angeles County Museum of Art. Public Domain CC0 image.