Skip to content

Commit e7bc457

Browse files
fix(server): keep initial viewer navigation responsive
1 parent 8edce26 commit e7bc457

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
152152
goto: vi.fn(async (next: string) => {
153153
url = next;
154154
events.emit("load");
155+
const pending = initialNavigation;
156+
initialNavigation = null;
157+
await pending?.();
155158
}),
156159
goBack: vi.fn(async () => {}),
157160
goForward: vi.fn(async () => {}),
@@ -206,6 +209,7 @@ let pageCopy = "";
206209
let keyCopy = "";
207210
let focusedFrame: ReturnType<typeof makeFrame> | null = null;
208211
let contextFailure: Error | null = null;
212+
let initialNavigation: (() => Promise<void>) | null = null;
209213
let desktopRendersNext = false;
210214
const desktopDetaches = new NodeEvents.EventEmitter();
211215
const desktopTabs = new Set<string>();
@@ -316,6 +320,7 @@ beforeEach(() => {
316320
contextRequests.length = 0;
317321
contextGate = null;
318322
contextFailure = null;
323+
initialNavigation = null;
319324
pageCopy = "";
320325
keyCopy = "";
321326
focusedFrame = null;
@@ -660,6 +665,41 @@ it.live.each([
660665
).pipe(Effect.provide(layer)),
661666
);
662667

668+
it.live("a user-opened tab allows a corrected URL before its initial navigation commits", () =>
669+
Effect.scoped(
670+
Effect.gen(function* () {
671+
const { browser } = yield* ready;
672+
const manager = yield* Manager.PreviewManager;
673+
yield* Effect.yieldNow;
674+
const started = Promise.withResolvers<void>();
675+
const committed = Promise.withResolvers<void>();
676+
yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
677+
initialNavigation = () => {
678+
started.resolve();
679+
return committed.promise;
680+
};
681+
const opened = yield* manager.open({
682+
threadId: scope.thread.threadId,
683+
url: "http://localhost:5173/stalled",
684+
runtime: "server",
685+
});
686+
yield* Effect.promise(() => started.promise);
687+
const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true));
688+
const correction = yield* viewer
689+
.input({ type: "navigate", url: "http://localhost:5173/fixed" })
690+
.pipe(Effect.forkScoped);
691+
yield* Effect.promise(() => new Promise<void>((resolve) => setImmediate(resolve)));
692+
const lastNavigationBeforeCommit = contexts[1]!.page.goto.mock.calls.at(-1);
693+
committed.resolve();
694+
yield* Fiber.join(correction);
695+
expect(lastNavigationBeforeCommit).toEqual([
696+
"http://localhost:5173/fixed",
697+
expect.objectContaining({ waitUntil: "commit" }),
698+
]);
699+
}),
700+
).pipe(Effect.provide(layer)),
701+
);
702+
663703
it.live("an expired queued click never reaches the page", () =>
664704
Effect.scoped(
665705
Effect.gen(function* () {

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,8 @@ const make = Effect.gen(function* () {
899899
: page.goto(snapshot.navStatus.url, options)
900900
).then(constVoid);
901901

902-
tab.control.track(tab.initialNavigation);
902+
if (tab.control.agentId !== null) tab.control.track(tab.initialNavigation);
903+
else void tab.initialNavigation.catch(constVoid);
903904
}
904905
return tab;
905906
};

‎apps/server/src/preview/SessionControl.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ export class SessionControl {
8181
this.assertOpen();
8282
if (epoch !== this.epoch || !allowed()) throw new BrowserControlInterrupted();
8383
if (deadlineMs !== undefined && performance.now() >= deadlineMs) {
84-
throw new DOMException("This browser request expired while queued.", "TimeoutError");
84+
throw new DOMException(
85+
"This browser request expired before it could start.",
86+
"TimeoutError",
87+
);
8588
}
8689
return run();
8790
});

0 commit comments

Comments
 (0)