WebContainer Integration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
WebContainer Integration is the core architectural component of the Bolt project, enabling a full-stack development environment to run entirely within the user's browser. By leveraging the StackBlitz WebContainer API, Bolt provides a sandboxed Node.js environment complete with a virtual file system, package manager, and terminal capabilities. This integration allows AI-generated code to be executed, previewed, and modified without the need for remote virtual machines or local software installations.
Sources: CONTRIBUTING.md:20-25, README.md:21-26
The integration acts as a bridge between the AI's instructions (artifacts and actions) and the browser runtime. It manages file synchronization, process execution (such as shell commands), and live previews of web applications. This enables a collaborative experience where both the AI assistant and the user can interact with the same live codebase in real-time.
Sources: CONTRIBUTING.md:28-30, app/lib/stores/workbench.ts:33-40
Architecture and Components
The WebContainer integration is managed through several specialized stores and runtime classes that interface with the @webcontainer/api.
Core Integration Stores
| Component | Description |
|---|---|
WorkbenchStore | The primary coordinator that initializes sub-stores and manages the global state of the development environment. |
FilesStore | Manages the virtual file system within the WebContainer, tracking file changes and handling UTF-8/binary detection. |
PreviewsStore | Monitors network ports opened within the WebContainer to provide live URLs for web applications. |
TerminalStore | Handles the attachment and resizing of Xterm.js terminals to the WebContainer's shell processes. |
ActionRunner | Executes specific commands (shell or file operations) within the WebContainer environment. |
Sources: app/lib/stores/workbench.ts:33-40, app/lib/stores/files.ts:33-40, app/lib/stores/previews.ts:12-15
Data Flow Logic
The system follows a reactive pattern using nanostores. When the AI generates a message, it is parsed into "Artifacts" and "Actions." These actions are then queued and executed by the ActionRunner inside the WebContainer instance.
The diagram above illustrates the flow from AI message generation to execution within the WebContainer and subsequent UI updates.
Sources: app/lib/runtime/message-parser.ts:50-70, app/lib/runtime/action-runner.ts:60-80, app/lib/stores/files.ts:98-105
File System Management
The FilesStore maintains a FileMap that reflects the state of the WebContainer's internal file system. It uses an internal watcher to monitor directory changes, file additions, and removals.
Virtual File System Operations
The system distinguishes between text and binary files using the istextorbinary utility. Text files are decoded using a TextDecoder for display in the editor, while binary files are flagged to prevent corruption.
- Watch Implementation: Uses
webcontainer.internal.watchPathsto track theWORK_DIR, excludingnode_modulesand.gitfor performance. - Path Sanitization: Removes trailing slashes to distinguish files from folders in the UI tree.
- Buffer Handling: Converts
Uint8Arraydata from the WebContainer into Node.js-compatible buffers for processing.
Sources: app/lib/stores/files.ts:100-115, app/lib/stores/files.ts:130-150, app/lib/stores/files.ts:192-205
Local Persistence Synchronization
A unique feature of this integration is the ability to sync the browser's WebContainer state with a local directory on the user's machine via the File System Access API.
This sequence ensures that changes made within the WebContainer environment are persisted to the user's actual local file system when a directory is selected.
Sources: app/lib/stores/workbench.ts:175-190, app/lib/persistence/fileSystem.ts:110-125
Action Execution Runtime
The ActionRunner is responsible for executing instructions defined in AI messages. It supports two primary action types: shell and file.
Action Lifecycle
- Pending: The action is added to the queue via
addAction. - Running: The action is currently executing (e.g.,
npm install). - Complete/Failed/Aborted: The final state after execution or user intervention.
Sources: app/lib/runtime/action-runner.ts:11-25
Shell and File Actions
Shell actions utilize webcontainer.spawn('jsh', ...) to run commands. The output is typically piped to the console or a terminal. File actions use webcontainer.fs.writeFile, ensuring that parent directories are created recursively if they do not exist.
// Example of Shell Action Execution in ActionRunner
async #runShellAction(action: ActionState) {
const webcontainer = await this.#webcontainer;
const process = await webcontainer.spawn('jsh', ['-c', action.content], {
env: { npm_config_yes: true },
});
// ... handle output and exit
}
Sources: app/lib/runtime/action-runner.ts:110-125, app/lib/runtime/action-runner.ts:135-150
Network and Previews
The PreviewsStore tracks network activity within the WebContainer. It listens for the port event, which indicates when a web server (like Vite or Express) has started inside the container.
Preview Tracking Logic
- Port Open: When a port opens, the store creates a
PreviewInfoobject containing the port number and the base URL (e.g., a.local.webcontainer.iodomain). - Port Close: When the server stops, the port is removed from the active previews list.
- Ready State: The
readyflag is toggled based on the container's event type (openorclose).
Sources: app/lib/stores/previews.ts:20-40
Configuration and Environment
WebContainer Integration requires specific browser headers and environment setup to function correctly.
| Configuration | File / Location | Description |
|---|---|---|
nodejs_compat | wrangler.toml | Enables Node.js compatibility for Cloudflare environment. |
WORK_DIR | app/utils/constants.ts | The root directory within the WebContainer (typically /home/project). |
webcontainer | app/lib/webcontainer/index.ts | The singleton instance of the WebContainer. |
Sources: wrangler.toml:3-5, app/lib/stores/files.ts:100, app/lib/stores/workbench.ts:8
Conclusion
The WebContainer Integration provides Bolt with a robust, browser-native development environment. By abstracting the complexities of file system watching, process management, and network port mapping into a set of reactive stores, the project allows users to go from a text prompt to a running, editable full-stack application instantly. This architecture ensures that all code execution remains secure and local to the user's browser session while maintaining the power of a standard Node.js development workflow.