diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.test.ts index 8800875e4b4..f8470a80d85 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.test.ts @@ -430,6 +430,86 @@ describe("browserRuntimeRegistry guest lifecycle", () => { throw new Error("Invalid guestInstanceId: 7"); }; + test("exposes a pending address without replacing the committed URL", async () => { + const paneId = "pending-address-pane"; + const entry = createEntry(paneId); + const navigation = Promise.withResolvers(); + const loadURL = mock(() => navigation.promise); + Object.assign(entry.webview, { loadURL }); + try { + browserRuntimeRegistry.navigate(paneId, "localhost:4000/slow"); + expect(loadURL).toHaveBeenCalledWith("http://localhost:4000/slow"); + expect(browserRuntimeRegistry.getState(paneId).pendingUrl).toBe( + "http://localhost:4000/slow", + ); + expect(browserRuntimeRegistry.getState(paneId).currentUrl).toBe( + "http://localhost:3000", + ); + fire(entry.webview, "did-navigate", { + url: "http://localhost:4000/redirected", + }); + expect(browserRuntimeRegistry.getState(paneId).pendingUrl).toBeNull(); + expect(browserRuntimeRegistry.getState(paneId).currentUrl).toBe( + "http://localhost:4000/redirected", + ); + } finally { + navigation.resolve(); + await navigation.promise; + registryInternals.entries.delete(paneId); + } + }); + + test("drops an aborted pending address while keeping the current page", async () => { + const paneId = "aborted-address-pane"; + const entry = createEntry(paneId); + const navigation = Promise.withResolvers(); + Object.assign(entry.webview, { loadURL: () => navigation.promise }); + const log = spyOn(console, "error").mockImplementation(() => {}); + try { + browserRuntimeRegistry.navigate(paneId, "http://localhost:4000/slow"); + navigation.reject(new Error("ERR_ABORTED")); + await navigation.promise.catch(() => {}); + expect(browserRuntimeRegistry.getState(paneId).pendingUrl).toBeNull(); + expect(browserRuntimeRegistry.getState(paneId).currentUrl).toBe( + "http://localhost:3000", + ); + } finally { + registryInternals.entries.delete(paneId); + log.mockRestore(); + } + }); + + test("an older aborted load cannot clear a newer pending address", async () => { + const paneId = "repeated-address-pane"; + const entry = createEntry(paneId); + const first = Promise.withResolvers(); + const second = Promise.withResolvers(); + let calls = 0; + Object.assign(entry.webview, { + loadURL: () => (calls++ === 0 ? first.promise : second.promise), + }); + const log = spyOn(console, "error").mockImplementation(() => {}); + try { + browserRuntimeRegistry.navigate(paneId, "http://localhost:4000/slow"); + browserRuntimeRegistry.navigate(paneId, "http://localhost:4000/slow"); + first.reject(new Error("ERR_ABORTED")); + await first.promise.catch(() => {}); + expect(browserRuntimeRegistry.getState(paneId).pendingUrl).toBe( + "http://localhost:4000/slow", + ); + fire(entry.webview, "did-navigate", { + url: "http://localhost:4000/slow", + }); + expect(browserRuntimeRegistry.getState(paneId).pendingUrl).toBeNull(); + } finally { + first.resolve(); + second.resolve(); + await Promise.allSettled([first.promise, second.promise]); + registryInternals.entries.delete(paneId); + log.mockRestore(); + } + }); + test("tracks the URL and title from events, not from the guest", () => { const paneId = "lifecycle-events-pane"; const entry = createEntry(paneId); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.ts index 8c721759c4c..728378b3c5b 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/browserRuntimeRegistry.ts @@ -17,6 +17,7 @@ const RAISED_OVERLAY_Z = "32"; export interface BrowserRuntimeState { currentUrl: string; + pendingUrl: string | null; pageTitle: string; faviconUrl: string | null; isLoading: boolean; @@ -49,6 +50,7 @@ interface RegistryEntry { */ overlay: HTMLDivElement; state: BrowserRuntimeState; + pendingNavigation: Promise | null; onPersist: ((state: PersistableBrowserState) => void) | null; /** Asks the pane to close itself; fired when the guest closes itself. */ onClose: (() => void) | null; @@ -75,6 +77,7 @@ const UNLOAD_INFO_TIMEOUT_MS = 5_000; const EMPTY_STATE: BrowserRuntimeState = Object.freeze({ currentUrl: "about:blank", + pendingUrl: null, pageTitle: "", faviconUrl: null, isLoading: false, @@ -390,6 +393,7 @@ export class BrowserRuntimeRegistryImpl { webview, overlay, state: { ...EMPTY_STATE, currentUrl: initialUrl }, + pendingNavigation: null, onPersist: null, onClose: null, workspaceId, @@ -457,6 +461,7 @@ export class BrowserRuntimeRegistryImpl { const handleDidNavigate = (e: Electron.DidNavigateEvent) => { this.setState(paneId, { currentUrl: e.url ?? "", + pendingUrl: null, pageTitle: "", faviconUrl: null, isLoading: false, @@ -775,9 +780,23 @@ export class BrowserRuntimeRegistryImpl { navigate(paneId: string, url: string): void { const entry = this.entries.get(paneId); if (!entry) return; - entry.webview.loadURL(sanitizeUrl(url)).catch((err) => { - console.error("[browserRuntimeRegistry] loadURL failed:", err); - }); + const targetUrl = sanitizeUrl(url); + const navigation = entry.webview.loadURL(targetUrl); + entry.pendingNavigation = navigation; + this.setState(paneId, { pendingUrl: targetUrl }); + navigation + .catch((err) => { + console.error("[browserRuntimeRegistry] loadURL failed:", err); + }) + .finally(() => { + if ( + this.entries.get(paneId) !== entry || + entry.pendingNavigation !== navigation + ) + return; + entry.pendingNavigation = null; + this.setState(paneId, { pendingUrl: null }); + }); } goBack(paneId: string): void { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/components/BrowserPaneToolbar/BrowserPaneToolbar.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/components/BrowserPaneToolbar/BrowserPaneToolbar.tsx index c7a14f5641e..46bdccd0ed1 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/components/BrowserPaneToolbar/BrowserPaneToolbar.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/components/BrowserPaneToolbar/BrowserPaneToolbar.tsx @@ -63,7 +63,7 @@ export function BrowserPaneToolbar({ ctx }: BrowserPaneToolbarProps) {