Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
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<void>();
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<void>();
const second = Promise.withResolvers<void>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const RAISED_OVERLAY_Z = "32";

export interface BrowserRuntimeState {
currentUrl: string;
pendingUrl: string | null;
pageTitle: string;
faviconUrl: string | null;
isLoading: boolean;
Expand Down Expand Up @@ -49,6 +50,7 @@ interface RegistryEntry {
*/
overlay: HTMLDivElement;
state: BrowserRuntimeState;
pendingNavigation: Promise<void> | null;
onPersist: ((state: PersistableBrowserState) => void) | null;
/** Asks the pane to close itself; fired when the guest closes itself. */
onClose: (() => void) | null;
Expand All @@ -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,
Expand Down Expand Up @@ -390,6 +393,7 @@ export class BrowserRuntimeRegistryImpl {
webview,
overlay,
state: { ...EMPTY_STATE, currentUrl: initialUrl },
pendingNavigation: null,
onPersist: null,
onClose: null,
workspaceId,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function BrowserPaneToolbar({ ctx }: BrowserPaneToolbarProps) {
<div className="@container/browser-toolbar flex h-full w-full min-w-0 items-center justify-between">
<BrowserToolbar
paneId={paneId}
currentUrl={state.currentUrl}
currentUrl={state.pendingUrl ?? state.currentUrl}
faviconUrl={state.faviconUrl}
isLoading={state.isLoading}
canGoBack={state.canGoBack}
Expand Down
Loading