WebContainer COOP/COEP Header Requirements for CDN-Hosted Low-Traffic Projects
0 reputation · 04 Sept 2021, 21:36 UTC
0 reputation · 04 Sept 2021, 21:36 UTC
StackBlitz WebContainers enable client-side Node.js execution through WebAssembly, shifting compute costs to the browser. For low-traffic workloads, hosting static project files on a CDN eliminates persistent server infrastructure. However, the WebContainer runtime requires Cross-Origin-Embedder-Policy (COEP) and Cross-Origin-Opener-Policy (COOP) headers to enable SharedArrayBuffer, which is essential for the virtualized POSIX environment.
Many CDN providers either strip custom headers by default or require specific configuration to propagate COEP: require-corp and COOP: same-origin across all responses. The WebContainer boot process also initiates cross-origin requests to the StackBlitz API for environment metadata, creating a tension between the strict header requirements and typical CDN caching behaviors.
The documentation specifies header requirements for the primary document but does not clarify whether subresource requests (WebAssembly modules, worker scripts, API calls) must carry identical headers or if the initial document headers are sufficient for the entire WebContainer lifecycle.
35275 reputation · 04 Sept 2021, 22:11 UTC
To enable SharedArrayBuffer required by StackBlitz WebContainers, only the top-level document that boots the runtime needs Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy. Confirmed fact: the browser creates a cross-origin isolated context from the entry HTML headers. Once isolated, the WebContainer lifecycle runs in that context.
Confirmed: the primary HTML document must return Cross-Origin-Opener-Policy: same-origin or same-origin-allow-popups and Cross-Origin-Embedder-Policy: require-corp. Applying these to the entry HTML is sufficient for the entire WebContainer session; COOP/COEP do not need to be repeated on WebAssembly modules, worker scripts, or API responses.
Likely explanation: subresources are loaded under the isolated context. With COEP: require-corp active, the browser blocks any cross-origin embeddable resource that does not opt-in. Therefore WASM/workers from another origin need Cross-Origin-Resource-Policy and CORS, not COOP/COEP.
Verification: open console and check self.crossOriginIsolated is true, and Network tab shows COOP/COEP on HTML only.
One missing diagnostic detail that changes recommendation: are your WebAssembly modules and worker scripts served from the same CDN domain and path as the entry HTML, or from a separate origin?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.