Skip to content

The agent that grows with you

fix(desktop): reuse preview webview guest across URL changes

finn763fb6715455877
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
index 006cd58021b..9f9ea77fd01 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
@@ -284,6 +284,45 @@ describe('PreviewPane console state', () => {
expect(goBack).not.toHaveBeenCalled()
})
+ // #120265: an external target.url change must steer the LIVE guest with
+ // loadURL(), not destroy the webview and rebuild it (which dropped JS
+ // state, cookies, form data, scroll, refs, and detached console/annotate).
+ it('reuses the live webview guest when target.url changes instead of rebuilding it', async () => {
+ const tabId = 'reuse-guest-tab'
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ tabId={tabId}
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5174/one', url: 'http://localhost:5174/one' }}
+ />
+ )
+ })
+
+ const first = rendered.container.querySelector('webview') as HTMLElement & Record<string, unknown>
+ expect(first).toBeInstanceOf(HTMLElement)
+ const loadURL = vi.fn(async () => undefined)
+ Object.assign(first, { loadURL })
+
+ await act(async () => {
+ rendered.rerender(
+ <PreviewPane
+ tabId={tabId}
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5174/two', url: 'http://localhost:5174/two' }}
+ />
+ )
+ })
+
+ // Same guest node: JS state, cookies, form data, scroll, and refs survive.
+ expect(rendered.container.querySelector('webview')).toBe(first)
+ // Steered with loadURL, not a src swap or a rebuild.
+ expect(loadURL).toHaveBeenCalledWith('http://localhost:5174/two')
+ expect(rendered.container.querySelector('webview')?.getAttribute('src')).toBe('http://localhost:5174/one')
+ expect((rendered.getByRole('textbox', { name: 'Address' }) as HTMLInputElement).value).toBe(
+ 'http://localhost:5174/two'
+ )
+ })
+
it('continues comment numbering in one conversation and resets it when the conversation changes', async () => {
$selectedStoredSessionId.set('session-one')
const selectedCrop = 'data:image/png;base64,c2VsZWN0ZWQ='
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
index a295515a7c2..0ab3a94b6cb 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
@@ -281,6 +281,11 @@ export function PreviewPane({
const [currentUrl, setCurrentUrl] = useState(target.url)
const liveUrlRef = useRef(currentUrl)
liveUrlRef.current = currentUrl
+ // The guest-creation effect below deliberately omits target.url from its
+ // deps (it reuses the guest across navigations — see #120265), so it reads
+ // the live address through this mirror instead of a stale closure.
+ const targetUrlRef = useRef(target.url)
+ targetUrlRef.current = target.url
const [devtoolsOpen, setDevtoolsOpen] = useState(false)
const [history, setHistory] = useState({ back: false, forward: false })
const [loading, setLoading] = useState(true)
@@ -1072,6 +1077,11 @@ export function PreviewPane({
}
}, [appendConsoleEntry, copy, reloadPreview, target.kind, target.url])
+ // The guest is created ONCE per preview kind and reused across URL changes
+ // within the session (#120265): an external target.url change steers the
+ // live guest with loadURL() in the sync effect below, instead of destroying
+ // the webview and rebuilding it (which dropped JS state, cookies, form
+ // data, scroll, refs, and detached the console/annotate channels).
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
useEffect(() => {
const host = hostRef.current
@@ -1080,9 +1090,10 @@ export function PreviewPane({
return
}
+ const initialUrl = targetUrlRef.current
host.replaceChildren()
webviewRef.current = null
- setCurrentUrl(target.url)
+ setCurrentUrl(initialUrl)
setDevtoolsOpen(false)
setHistory({ back: false, forward: false })
setLoadError(null)
@@ -1098,7 +1109,7 @@ export function PreviewPane({
const webview = document.createElement('webview') as PreviewWebview
webview.className = 'flex h-full w-full flex-1 bg-transparent'
webview.setAttribute('partition', 'persist:hermes-preview')
- webview.setAttribute('src', target.url)
+ webview.setAttribute('src', initialUrl)
webview.setAttribute('webpreferences', 'contextIsolation=yes,nodeIntegration=no,sandbox=yes')
// The guest preload (main.ts installs it on this partition) forwards a
@@ -1140,7 +1151,7 @@ export function PreviewPane({
if ((detail.level ?? 0) >= 3 && isModuleMimeError(message)) {
setLoadError({
description: copy.moduleMimeDescription,
- url: guestPage(webview, target.url).url
+ url: guestPage(webview, liveUrlRef.current).url
})
setLoading(false)
}
@@ -1163,7 +1174,7 @@ export function PreviewPane({
return
}
- noteBrowserPage(tabId, guestPage(webview, target.url))
+ noteBrowserPage(tabId, guestPage(webview, liveUrlRef.current))
}
const onNavigate = (event: Event) => {
@@ -1203,7 +1214,7 @@ export function PreviewPane({
setLoadError({
code: errorCode,
description: detail.errorDescription || copy.unreachableDescription,
- url: detail.validatedURL || guestPage(webview, target.url).url
+ url: detail.validatedURL || guestPage(webview, liveUrlRef.current).url
})
setLoading(false)
}
@@ -1343,7 +1354,54 @@ export function PreviewPane({
webview.remove()
setAnnotate(session => (session.mode ? { ...endAnnotateMode(session), stack: emptyAnnotateStack() } : session))
}
- }, [appendConsoleEntry, consoleState, copy, isRemoteHtml, isWebPreview, tabId, target.kind, target.url])
+ }, [appendConsoleEntry, consoleState, copy, isRemoteHtml, isWebPreview, tabId, target.kind])
+
+ // Steers the LIVE guest when the session opens a new URL (#120265): loadURL
+ // keeps the webview instance (JS state, cookies, form data, scroll, refs,
+ // console/annotate channels) where the effect above used to destroy and
+ // rebuild it. Per-page state (annotate pins, console log, history) resets
+ // around the load so nothing bleeds across pages. Skipped when the guest
+ // already shows the address (an in-page navigation got there first).
+ // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
+ useEffect(() => {
+ if (!isWebPreview || isRemoteHtml) {
+ return
+ }
+
+ const webview = webviewRef.current
+
+ if (!webview?.loadURL) {
+ return
+ }
+
+ const nextUrl = targetUrlRef.current
+
+ if (liveUrlRef.current === nextUrl) {
+ return
+ }
+
+ annotateLoopRef.current += 1
+ const guest = annotateGuest()
+
+ if (guest) {
+ void teardownAnnotateOverlay(guest).catch(() => undefined)
+ }
+
+ setDraftNote('')
+ setAnnotate(emptyAnnotateSession())
+ setLoadError(null)
+ consoleState.reset()
+ setHistory({ back: false, forward: false })
+ setLoading(true)
+ setCurrentUrl(nextUrl)
+ void webview.loadURL?.(nextUrl)?.catch((error: unknown) => {
+ setLoadError({
+ description: error instanceof Error ? error.message : copy.unreachableDescription,
+ url: nextUrl
+ })
+ setLoading(false)
+ })
+ }, [annotateGuest, consoleState, copy.unreachableDescription, isRemoteHtml, isWebPreview, target.url])
return (
<aside