🎓 A1Learner
🔍
0 of 50 lessons completed

WebAssembly Cross-Origin Isolation (COOP/COEP)

Cross-origin isolation gates SharedArrayBuffer in the browser. Two headers switch it on.

Send both headers

Set each header on every response.

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

Check the mode in JavaScript

This reads true only when both headers are set.

console.log(crossOriginIsolated);

Example

const B64 = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=";
(async () => {
  const bytes = Uint8Array.from(atob(B64), c => c.charCodeAt(0));
  const { instance } = await WebAssembly.instantiate(bytes, {});
  console.log("2 + 3 =", instance.exports.add(2, 3));
})();

Key points

  • Cross-origin isolation is a mode that guards shared memory.
  • Use Cross-Origin-Opener-Policy with the value same-origin.
  • Use Cross-Origin-Embedder-Policy with the value require-corp.
  • COOP is short for Cross-Origin-Opener-Policy.
  • COEP is short for Cross-Origin-Embedder-Policy.
  • A threaded module fails without both headers, in development and in production.
  • The loader below works without isolation, threads do not.
  • Two headers switch on the mode threads need.

In this example, the code decodes the module bytes from B64 and instantiates the module. It calls add with 2 and 3 and logs the sum. The loader needs no headers, but threads do.

Note

A page without both headers reports crossOriginIsolated as false.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

Which headers switch on cross-origin isolation?