mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:50:45 +00:00
fix(browser): ignore handled route navigation races
Co-authored-by: Richard Steadman <198648604+Steady-ai@users.noreply.github.com>
This commit is contained in:
@@ -345,6 +345,34 @@ describe("pw-session createPageViaPlaywright navigation guard", () => {
|
||||
expect(pageClose).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ignores already-handled route races during guarded navigation", async () => {
|
||||
const { pageGoto, pageClose, getRouteHandler, mainFrame } = installBrowserMocks();
|
||||
const route = createMockRoute({
|
||||
continue: vi.fn(async () => {
|
||||
throw new Error("Route is already handled");
|
||||
}),
|
||||
});
|
||||
pageGoto.mockImplementationOnce(async () => {
|
||||
await dispatchMockNavigation({
|
||||
getRouteHandler,
|
||||
mainFrame,
|
||||
url: "https://example.com",
|
||||
route,
|
||||
});
|
||||
return null;
|
||||
});
|
||||
|
||||
const created = await createPageViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
url: "https://example.com",
|
||||
});
|
||||
|
||||
expect(created.targetId).toBe("TARGET_1");
|
||||
expect(route.continue).toHaveBeenCalledTimes(1);
|
||||
expect(pageGoto).toHaveBeenCalledTimes(1);
|
||||
expect(pageClose).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("propagates unsupported redirect protocols as navigation errors", async () => {
|
||||
const { pageGoto, pageClose, getRouteHandler, mainFrame } = installBrowserMocks();
|
||||
mockBlockedRedirectNavigation({
|
||||
|
||||
@@ -818,6 +818,18 @@ export async function assertPageNavigationCompletedSafely(
|
||||
}
|
||||
}
|
||||
|
||||
async function continueRouteSafely(route: Route): Promise<void> {
|
||||
try {
|
||||
await route.continue();
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "";
|
||||
if (message.includes("Route is already handled")) {
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function gotoPageWithNavigationGuard(
|
||||
opts: {
|
||||
cdpUrl: string;
|
||||
@@ -841,7 +853,7 @@ export async function gotoPageWithNavigationGuard(
|
||||
const isSubframeDocument =
|
||||
!isTopLevel && isSubframeDocumentNavigationRequest(opts.page, request);
|
||||
if (!isTopLevel && !isSubframeDocument) {
|
||||
await route.continue();
|
||||
await continueRouteSafely(route);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -859,7 +871,7 @@ export async function gotoPageWithNavigationGuard(
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
await route.continue();
|
||||
await continueRouteSafely(route);
|
||||
};
|
||||
|
||||
await opts.page.route("**", handler);
|
||||
|
||||
Reference in New Issue
Block a user