Skip to content
All posts

Sep 20, 2026 · 1 min read

Running AI-generated code in the browser with WebContainer

Generating code with an AI model is only half the experience. The magic moment is when that code runs immediately — no copying files, no local setup. That's what I set out to build with SOEN, my AI Software Engineer project.

The stack

What WebContainer does

WebContainer runs Node.js inside the browser tab. You give it a file tree, and it can install packages and start a dev server — all client-side. The core flow looks like this:

import { WebContainer } from "@webcontainer/api";

const container = await WebContainer.boot();
await container.mount(files); // the generated project

const install = await container.spawn("npm", ["install"]);
await install.exit;

await container.spawn("npm", ["run", "dev"]);
container.on("server-ready", (port, url) => {
  previewFrame.src = url; // show the running app
});

Lessons learned

1. Structure beats free text. Code is much easier to use when the AI's output has a predictable shape (for example, a list of files and their contents) rather than a loose block of text you have to parse.

2. Browser security headers matter. WebContainer relies on cross-origin isolation, so the server has to send the right Cross-Origin-Embedder-Policy and Cross-Origin-Opener-Policy headers. Missing them is a common first stumbling block.

3. Show progress. Installing packages takes time. Streaming terminal output to the user makes the wait feel intentional instead of broken.

4. Keep the human in control. AI output should be a starting point that users can read, edit and re-run — not a black box.

Why it matters

Tools like this shorten the loop between idea and running app. Building one taught me a lot about AI APIs, in-browser runtimes, auth and real-time systems — all in a single project.

Thanks for reading!

Want to work together or have a question? Get in touch.