Skip to content

Commit a053025

Browse files
authored
fix(ui): preserve navigation during startup (#114828)
1 parent feb5883 commit a053025

3 files changed

Lines changed: 177 additions & 107 deletions

File tree

‎ui/src/app/bootstrap.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { RouteLocation } from "@openclaw/uirouter";
22
import { describe, expect, it, vi } from "vitest";
33
import type { GatewayBrowserClient } from "../api/gateway.ts";
4-
import type { RouteId } from "../app-routes.ts";
4+
import { routeIdFromPath, type RouteId } from "../app-routes.ts";
55
import { sessionRefFromPath } from "../app-session-route-paths.ts";
6-
import { startModelSetupFirstRunRedirectAfterLocation } from "../pages/model-setup/first-run.ts";
6+
import {
7+
isDefaultChatLanding,
8+
startModelSetupFirstRunRedirectAfterLocation,
9+
} from "../pages/model-setup/first-run.ts";
710
import {
811
normalizeInitialApplicationLocation,
912
resolveInitialApplicationLocation,
@@ -367,6 +370,56 @@ describe("normalizeInitialApplicationLocation", () => {
367370
expect(replaceRoute).toHaveBeenCalledWith("model-setup", { search: "?firstRun=1" });
368371
});
369372

373+
it("does not replace a user route with the deferred default chat location", async () => {
374+
const currentLocation = { pathname: "/new", search: "", hash: "" };
375+
const installLocation = vi.fn();
376+
377+
await startModelSetupFirstRunRedirectAfterLocation({
378+
context: {} as ApplicationContext<RouteId>,
379+
enabled: false,
380+
history: { location: () => currentLocation, replace: vi.fn() },
381+
initialLocationReady: Promise.resolve({ pathname: "/chat/main", search: "", hash: "" }),
382+
installLocation,
383+
shouldInstallLocation: () => isDefaultChatLanding(currentLocation, "", routeIdFromPath),
384+
});
385+
386+
expect(installLocation).not.toHaveBeenCalled();
387+
});
388+
389+
it("keeps the latest navigation requested before router start", async () => {
390+
const previousSettings = loadSettings();
391+
const previousUrl = window.location.href;
392+
saveSettings({
393+
...previousSettings,
394+
sessionKey: "agent:main:main",
395+
lastActiveSessionKey: "agent:main:main",
396+
});
397+
window.history.replaceState({}, "", "/chat");
398+
const sessionPathBuilder = deferred<void>();
399+
const runtime = bootstrapApplication({ sessionPathBuilderReady: sessionPathBuilder.promise });
400+
const pushState = vi.spyOn(window.history, "pushState");
401+
402+
try {
403+
const start = runtime.start();
404+
runtime.context.replace("about");
405+
runtime.context.navigate("new-session");
406+
expect(window.location.pathname).toBe("/chat");
407+
408+
sessionPathBuilder.resolve();
409+
await start;
410+
411+
expect(runtime.router.getState().matches[0]?.routeId).toBe("new-session");
412+
expect(runtime.router.getState().resolvedLocation?.pathname).toBe("/new");
413+
expect(window.location.pathname).toBe("/new");
414+
expect(pushState).toHaveBeenCalledWith({}, "", "/new");
415+
} finally {
416+
pushState.mockRestore();
417+
runtime.stop();
418+
saveSettings(previousSettings);
419+
window.history.replaceState({}, "", previousUrl);
420+
}
421+
});
422+
370423
it("does not restart routing after stop wins the session-path loader race", async () => {
371424
const previousSettings = loadSettings();
372425
const previousUrl = window.location.href;

‎ui/src/app/bootstrap.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { RouteLocation } from "@openclaw/uirouter";
12
import type { GatewayBrowserClient } from "../api/gateway.ts";
23
import { sessionRouteNamespaceFromPath } from "../app-route-paths.ts";
34
import {
@@ -232,6 +233,12 @@ type BootstrapApplicationDependencies = {
232233
sessionPathBuilderReady?: Promise<void>;
233234
};
234235

236+
type PendingRouterStartNavigation = {
237+
routeId: RouteId;
238+
location: RouteLocation;
239+
mode: "push" | "replace";
240+
};
241+
235242
export function bootstrapApplication(
236243
dependencies: BootstrapApplicationDependencies = {},
237244
): ApplicationRuntime {
@@ -344,6 +351,10 @@ export function bootstrapApplication(
344351
const initialUserMessage = createInitialUserMessageHandoff();
345352
applyThemePresentation(settings);
346353
const router = createApplicationRouter();
354+
let routerStarted = false;
355+
// Pre-start navigations are invisible to history; retain the latest request so
356+
// router.start() cannot resolve the stale browser URL over the user's route.
357+
let pendingRouterStartNavigation: PendingRouterStartNavigation | null = null;
347358
let pendingGatewayConnection =
348359
startup.pendingGatewayUrl !== null
349360
? {
@@ -430,15 +441,23 @@ export function bootstrapApplication(
430441
skillWorkshopRevision,
431442
initialUserMessage,
432443
navigate: (routeId, options) => {
444+
const location = routeLocation(routeId, options);
445+
if (!routerStarted) {
446+
pendingRouterStartNavigation = { routeId, location, mode: "push" };
447+
}
433448
void router
434-
.navigate(routeId, context, { history: "push" }, routeLocation(routeId, options))
449+
.navigate(routeId, context, { history: "push" }, location)
435450
.catch((error: unknown) => {
436451
console.error("[openclaw] route navigation failed", error);
437452
});
438453
},
439454
replace: (routeId, options) => {
455+
const location = routeLocation(routeId, options);
456+
if (!routerStarted) {
457+
pendingRouterStartNavigation = { routeId, location, mode: "replace" };
458+
}
440459
void router
441-
.navigate(routeId, context, { history: "replace" }, routeLocation(routeId, options))
460+
.navigate(routeId, context, { history: "replace" }, location)
442461
.catch((error: unknown) => {
443462
console.error("[openclaw] route replacement failed", error);
444463
});
@@ -482,6 +501,12 @@ export function bootstrapApplication(
482501
});
483502
if (!documentMode) {
484503
steps.push(async () => {
504+
const pendingNavigation = pendingRouterStartNavigation;
505+
pendingRouterStartNavigation = null;
506+
routerStarted = true;
507+
if (pendingNavigation) {
508+
history[pendingNavigation.mode](pendingNavigation.location);
509+
}
485510
await startApplicationRouter(router, history, basePath, context);
486511
return stopRouter;
487512
});

0 commit comments

Comments
 (0)