Platform
Cross-origin isolation
The page that embeds a sandbox must be cross-origin isolated. Two response headers, COOP and COEP, turn it on. Without them the browser withholds the features the runtime is built on.
Why isolation is required
Browsers only expose SharedArrayBuffer and Atomics.wait to documents that are cross-origin isolated, a mode that guarantees no other site's data shares the process. A page enters it by sending two headers:
Cross-Origin-Opener-Policy: same-originCOOP. Puts the page in its own browsing context group, cutting window references to and from cross-origin popups and openers.Cross-Origin-Embedder-Policy: credentiallessCOEP. Every cross-origin resource the page loads must either opt in (CORS or a Cross-Origin-Resource-Policy header) or, with credentialless, is fetched without cookies. require-corp is the stricter value.When both are present on a secure origin (HTTPS or localhost), self.crossOriginIsolated is true and shared memory becomes available.
How NodeSandbox uses it
The runtime does not run in your page. Sandbox.create() inserts a small, hidden iframe pointing at the runtime host, a separate origin per session, and untrusted project code runs there. The runtime host already serves its own COEP and CORP headers, and the client marks the iframe with allow="cross-origin-isolated" to delegate isolation to it.
A frame can only be isolated when the top-level page is, though. Isolation is decided by the top-level document's headers, so the runtime frame inherits it from your page or doesn't get it at all. That is the only part you configure.
Inside the runtime, shared memory is what lets the browser behave like an operating system:
Worker processesEach node, npm, or dev-server process runs in its own Web Worker. Synchronous Node APIs (fs.readFileSync, child_process.execSync, Atomics-based waits) block on shared memory while another thread answers.Real npmDependencies install with the npm CLI itself, which needs worker processes.Threaded WebAssemblyRolldown (Vite), Tailwind's Oxide, Lightning CSS, and the tsc command compile with threaded WASI.Bun and PythonThe bun command and Python (pip, servers) run as worker processes.AgentsClaude Code needs SharedArrayBuffer. Codex runs through Wasmer in your own page, so it needs your page itself to be isolated.An <iframe credentialless> gets an ephemeral storage partition with no service worker, and the preview depends on the runtime's service worker. The client, React, and Elements create their frames with allow="cross-origin-isolated" instead. If you render a preview iframe yourself, do the same.
Headers to send
Send both headers on the HTML document that embeds the sandbox. Use credentialless as the COEP value: it isolates the page without requiring every CDN, font host, and image server you use to send Cross-Origin-Resource-Policy. Scope the headers to the routes that host a sandbox if the rest of your site embeds third-party content.
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
async headers() {
return [
{
source: '/editor/:path*', // the routes that embed a sandbox
headers: [
{ key: 'Cross-Origin-Opener-Policy', value: 'same-origin' },
{ key: 'Cross-Origin-Embedder-Policy', value: 'credentialless' },
],
},
];
},
};
export default nextConfig;import { defineConfig } from 'vite';
const isolation = {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless',
};
export default defineConfig({
server: { headers: isolation }, // vite dev
preview: { headers: isolation }, // vite preview
});The dev server's headers don't carry over to production. Configure your host too: _headers on Cloudflare Pages or Netlify, headers in vercel.json, or add_header in nginx.
/editor/*
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: credentiallessSafari and iOS
WebKit has never implemented credentialless. Safari accepts the header, ignores the value, and leaves the page unisolated, so crossOriginIsolated is false even with both headers present. WebKit does support require-corp. Every browser on iOS is WebKit, Chrome and Firefox included.
To support Safari, choose the COEP value per request from the user agent. Send require-corp to WebKit and credentialless to everything else. The runtime host does the same on its side. COOP stays same-origin for all browsers.
import { NextResponse, type NextRequest } from 'next/server';
function coepFor(userAgent: string | null): string {
if (!userAgent) return 'credentialless';
// Every iOS browser is WebKit, whatever its name.
if (/\b(iPhone|iPad|iPod)\b/.test(userAgent)) return 'require-corp';
// iPadOS reports a desktop Mac user agent.
if (/Macintosh/.test(userAgent) && /Mobile\//.test(userAgent)) return 'require-corp';
// Desktop Safari. Chrome and Edge also say "Safari", so exclude them.
if (/Safari\//.test(userAgent) && !/(Chrome|Chromium|Android|Edg)\//.test(userAgent)) {
return 'require-corp';
}
return 'credentialless';
}
export function proxy(request: NextRequest) {
const response = NextResponse.next();
response.headers.set('Cross-Origin-Embedder-Policy', coepFor(request.headers.get('user-agent')));
return response;
}
export const config = { matcher: ['/editor/:path*'] };Compute the header for each request (middleware, an edge function, or a server handler). If a CDN caches the page, add Vary: User-Agent or bypass the cache, or one browser's value will be served to another. Under require-corp, every cross-origin image, script, font, and stylesheet on the page must send Cross-Origin-Resource-Policy: cross-origin or be loaded with crossorigin from a host that sends CORS headers. Test these pages in Safari specifically.
Effects on your page
The headers apply to your whole document, not only the sandbox. These are the effects to plan for:
Cross-origin subresourcesWith credentialless, images, scripts, and fonts from other origins still load, but no-cors requests are sent without cookies. Assets that depend on a third-party login cookie won't work.Third-party iframesA cross-origin iframe (a YouTube embed, a map, a payment widget) is blocked unless it sends its own COEP, which few do. Add the credentialless attribute to those iframes in Chromium, or keep them on pages without the headers.Popups and OAuthCOOP same-origin severs window.opener between your page and cross-origin popups, so popup sign-in flows that postMessage back to the opener break. Use a redirect flow, or start sign-in from a page that doesn't send COOP. same-origin-allow-popups does not isolate the page.Analytics and adsTags that inject cross-origin iframes or rely on third-party cookies may stop reporting on isolated routes.Your users' apps get the same treatment inside the preview. The preview is served with credentialless COEP, so the runtime adds the credentialless attribute to cross-origin iframes the app creates. Embeds in a previewed app then load in Chromium. Firefox and Safari have no credentialless iframes, so there those embeds stay blocked.
Without isolation
If your page doesn't send the headers, the runtime iframe still loads but has no shared memory. The runtime falls back to an in-process mode, and many projects won't run:
- Vite (Rolldown) projects stop at boot with
This Vite/Rolldown project requires a cross-origin-isolated runtime iframe. - Dependencies install with a built-in installer instead of real npm.
bun, Python,tsc, and threaded WebAssembly tooling are unavailable.- Child processes run inline on one thread, and agent plugins that need shared memory fail to start.
Treat the fallback as a diagnostic, not a supported configuration. Send the headers on every route that embeds a sandbox.
Verify and troubleshoot
Open the page that embeds the sandbox and check its console:
crossOriginIsolated // must be true on your pagecrossOriginIsolated is false in ChromeA header is missing from the HTML document itself, often because only the dev server sets it, or a CDN or framework strips it. Check the document's response headers in the Network panel.false only in Safari or on iPhoneCOEP is credentialless. Send require-corp to WebKit.Vite/Rolldown project requires a cross-origin-isolated runtime iframeYour page isn't isolated, so the runtime frame can't be either.Images, fonts, or a widget disappear after adding the headersA cross-origin resource needs CORP or CORS (always under require-corp), or a third-party iframe needs the credentialless attribute.Preview shows the browser's blocked-frame pageThe preview frame has the credentialless attribute, so the runtime's service worker can't serve it. Render previews through the SDK, or with allow="cross-origin-isolated".Isolated on reload, not when navigated toHeaders apply when a document loads. A client-side navigation (a Next.js Link, a router push) from an unisolated route keeps the unisolated document. Link to sandbox routes with a full page load.See Create a sandbox for the rest of the setup.