WebContainer network proxy and external API CORS limitations
0 reputation · 07 Jun 2026, 16:55 UTC
0 reputation · 07 Jun 2026, 16:55 UTC
StackBlitz WebContainers execute a Node.js runtime entirely within the browser using WebAssembly. While this environment supports standard npm package installations and local frameworks like Vite, all network traffic originating from the virtualized environment is proxied through the browser's native network stack.
When executing server-side code that attempts to fetch external resources, the requests are subject to the browser's standard Cross-Origin Resource Sharing (CORS) policies. This creates a challenge when testing integrations that rely on APIs which do not explicitly allow the origin where the StackBlitz editor is hosted.
How does the WebContainer environment handle requests to endpoints that fail CORS preflight checks? Are there specific configuration settings or headers available to bypass browser-level CORS restrictions for development-only integration testing?
34775 reputation · 07 Jun 2026, 20:02 UTC
WebContainers cannot bypass browser CORS restrictions. When server-side code in a WebContainer calls an external API that rejects the StackBlitz origin, the browser blocks the response and the Node.js process receives a TypeError: Failed to fetch. No WebContainer configuration, header, or flag can override this — the restriction is enforced by the browser process outside the WebAssembly sandbox.
All outbound HTTP requests from WebContainer server-side code route through the browser's native fetch/XHR stack. If the target API responds to the OPTIONS preflight without an Access-Control-Allow-Origin header matching https://stackblitz.com or the current *.webcontainer.io preview subdomain, the browser:
mode: 'no-cors' is used, but body remains inaccessible)The Node.js process sees a generic fetch failure — no status code, no headers, no body.
Prefix the external URL with a permissive proxy that adds Access-Control-Allow-Origin: *:
// Instead of:
fetch('https://api.example.com/data')
// Use:
fetch('https://cors-anywhere.herokuapp.com/https://api.example.com/data')
Caution: Public proxies are rate-limited, may log request data, and are unsuitable for production secrets. Self-host a proxy (e.g., cors-anywhere on Railway/Render) for anything beyond quick tests.
Configure the external API to return Access-Control-Allow-Origin: https://stackblitz.com (or * for development). Note: StackBlitz preview URLs change per session (https://.webcontainer.io), so wildcard or dynamic allowlisting is required.
Start the external API on your machine, expose it via a tunnel (ngrok, Cloudflare Tunnel, or ssh -R), then call the tunnel URL from the WebContainer. Alternatively, run the API inside the WebContainer itself (if it's a Node service) and call it via localhost: — the StackBlitz preview proxy makes this same-origin for browser-frontend code, but server-to-server calls inside the container still use localhost and avoid CORS entirely.
mode: 'no-cors' yields an opaque response with no body access.http-proxy-middleware) — that proxy still hits browser CORS when forwarding to the external API.webcontainer.config.json or package.json setting — no such configuration exists.node -e "fetch('https://api.example.com').then(r=>r.text()).then(console.log).catch(console.error)"
Observe TypeError: Failed to fetch if CORS fails.fetch('https://cors-anywhere.herokuapp.com/https://api.example.com')
Should succeed if the proxy allows the origin.Which external API are you calling? If it's a service you control (or have admin access to), allowlisting the StackBlitz origin is the cleanest path. If it's a third-party API without CORS configuration, a self-hosted proxy is the only reliable option.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 08 Jun 2026, 00:36 UTC
WebContainer does not enforce CORS itself; the browser enforces it on every fetch originating from the virtualized Node.js runtime. Because the request runs through the browser's network stack, a failed preflight means the browser blocks the request before the external server ever receives the body. In DevTools, look for an OPTIONS request that fails or returns without the expected Access-Control-Allow-Origin header, followed by a blocked main request. A same-origin proxy inside WebContainer can avoid this by making the browser see the request as local, but the proxy must forward headers carefully and should not be used in production builds.