How to fix SharedArrayBuffer errors when hosting a browser game
Updated · 2 min read
If your Godot 4, Bevy, Unity WebGL, or threaded WebAssembly game works on localhost and crashes online with ReferenceError: SharedArrayBuffer is not defined or Cross-Origin Isolation is required, your web host is missing two HTTP response headers:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Browsers lock SharedArrayBuffer (required for multi-threaded WASM and Web Workers sharing memory) behind cross-origin isolation to mitigate Spectre-class side-channel attacks. You can verify whether a page is isolated by checking window.crossOriginIsolated in DevTools—it must return true.
How to add COOP/COEP headers on common hosts
Vite (local development)
Add headers to vite.config.ts so local dev matches production:
export default {
server: {
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
preview: {
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
};
Cloudflare Pages and Netlify (_headers)
Put a _headers file in your public/ (or build output) directory:
/*
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Vercel (vercel.json)
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Cross-Origin-Opener-Policy", "value": "same-origin" },
{ "key": "Cross-Origin-Embedder-Policy", "value": "require-corp" }
]
}
]
}
Nginx
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Embedder-Policy "require-corp" always;
itch.io
In your project's Edit game → Embed options, check SharedArrayBuffer support (experimental). Note that itch.io's setting can conflict with certain third-party embeds and only applies to the game iframe.
GitHub Pages
GitHub Pages does not let you set custom HTTP response headers. You either need to disable multi-threading in your engine export (for example, Godot 4.3+'s single-threaded web export), inject a service-worker workaround like coi-serviceworker, or move the build to a host that supports custom headers.
Pita
On Pita, every game runs on its own isolated subdomain (<slug>.games.pita.sh) with COOP/COEP cross-origin isolation headers enabled automatically—and pita dev serves http://localhost:7070 cross-origin isolated locally too, so dev matches production with no config file needed.
Common gotcha after turning on COEP
Once Cross-Origin-Embedder-Policy: require-corp is enabled, browsers will block external images, fonts, or scripts loaded from another domain unless that domain sends Cross-Origin-Resource-Policy: cross-origin or valid CORS headers. Bundle your fonts, audio, and textures locally inside your build folder rather than hotlinking CDNs.
FAQ
Can I use credentialless instead of require-corp?
Cross-Origin-Embedder-Policy: credentialless works in Chrome and Firefox and relaxes the rule for third-party assets, but Safari has historically lagged on credentialless support. Bundling assets locally and using require-corp is the safest cross-browser setup.
Does my Three.js or Canvas 2D game need SharedArrayBuffer?
Usually no, unless you use a multi-threaded WebAssembly physics engine, audio worklet, or custom SharedArrayBuffer workers. Godot 4 (threaded), Bevy (threaded), and Unity WebGL (threads enabled) do require it.