Web Preview
Show a page an agent built in a sandboxed frame, with a browser bar, device widths, and a console.
Overview
Web Preview shows a running page next to the conversation that made it: a dev server URL, a deployed preview, or HTML the agent wrote. The page loads in a sandboxed frame under a slim bar with history, reload, the address, a device width, and a link to open it in a new tab.
It shows the state you pass. It does not run a server or record history: wire onBack, onForward, and onNavigate to your own, and pass canGoBack and canGoForward. Pass logs to show what the page printed, such as messages your sandbox forwards from the page's console.
Anatomy
- BarBack, Forward, Reload, the address, the device widths, and Open in new tab
- ViewportThe sandboxed frame, narrowed to the chosen device width
- ConsoleA drawer of logged messages, when logs are passed
Behavior
Loading and errors
Until the frame reports that it loaded, the viewport shows a loading state and the address bar shows a spinner. Pass status when your app knows more than the frame does, such as a build that is still running. With status="error", the page is hidden behind a message, your error text, and Try again, which reloads the frame.
Address
Submitting the address calls onNavigate with the new value. Escape puts the current address back. Without onNavigate, the address is read-only and can still be selected and copied.
Reload
Reload remounts the frame, so it works for srcDoc pages as well as URLs, then calls onReload.
Device width
Desktop fills the viewport. Tablet narrows the frame to 768px and Mobile to 390px, centered, so you can check a layout without resizing the window. The widths hide on narrow screens, where the viewport is already small.
Sandbox
The frame's default sandbox allows scripts and forms only. The page cannot reach the hosting page, its storage, or its cookies, open popups, or navigate the top window. Widen sandbox only for pages you trust: adding both allow-scripts and allow-same-origin lets a same-origin page remove its own sandbox.
Console
With logs, a Console row sits under the page with the message count and, when there are errors, the error count. It opens to a scrolling list that keeps the newest message in view. Warnings and errors are colored; other messages are not.
Accessibility
The frame is named by title. Each bar button has a name, and the device widths are a group of toggle buttons that report which is pressed. The loading state is a status and the error is an alert. The console toggle reports whether it is open, and the open list can take focus so it can be scrolled from the keyboard.
Installation
Usage
import { WebPreview } from "@/components/ai/web-preview"
<WebPreview url={server.url} title="App preview" />
<WebPreview url="Generated page" srcDoc={artifact.html} title="Generated page" />
<WebPreview
url={location}
title="App preview"
onNavigate={setLocation}
canGoBack={history.canGoBack}
onBack={history.back}
logs={consoleMessages}
/>Examples
Error
The dev server stopped, so the page is replaced by the reason and Try again.
API reference
WebPreview
Accepts native div props beyond those listed.