Skip to content

Commit fbe137d

Browse files
camchismaria-rcks
andauthored
fix(server): preview tab list shows desktop tabs that loaded before the server attached (#27456)
Co-authored-by: maria-rcks <[email protected]>
1 parent 6d584fa commit fbe137d

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
5858
return makeEncoderPage() as unknown as Page;
5959
}
6060
async connectDesktopPage(endpoint: string) {
61-
const context = makeContext();
61+
const context = makeContext(undefined, desktopPageUrl);
6262
context.page.viewportSize.mockReturnValue(null);
6363
desktopConnections.push({ endpoint, context });
6464
return { browser: { close: async () => {} }, page: context.page as unknown as Page };
@@ -151,12 +151,12 @@ function fileChooserElement(accept: string) {
151151
};
152152
}
153153

154-
function makeContext(onClose?: (context: BrowserContext) => void) {
154+
function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "about:blank") {
155155
const events = new NodeEvents.EventEmitter();
156156
const nativeFileChooser = vi.fn();
157157
const setChooserFiles = vi.fn(async (_files: unknown) => {});
158158
const sessions: ReturnType<typeof makeSession>[] = [];
159-
let url = "about:blank";
159+
let url = initialUrl;
160160
let viewport: ReturnType<Page["viewportSize"]> = { width: 1280, height: 800 };
161161
const cssViewport = { width: 1920, height: 1080 };
162162
let closed = false;
@@ -299,6 +299,7 @@ let focusedFrame: ReturnType<typeof makeFrame> | null = null;
299299
let contextFailure: Error | null = null;
300300
let initialNavigation: (() => Promise<void>) | null = null;
301301
let desktopRendersNext = false;
302+
let desktopPageUrl = "about:blank";
302303
/** Replaces the answer to the desktop page's size reads. */
303304
let desktopSizeAnswer: (() => Promise<Record<string, unknown>>) | null = null;
304305
const desktopDetaches = new NodeEvents.EventEmitter();
@@ -446,6 +447,7 @@ beforeEach(() => {
446447
focusedFrame = null;
447448
desktopTabs.clear();
448449
desktopRendersNext = false;
450+
desktopPageUrl = "about:blank";
449451
desktopSizeAnswer = null;
450452
desktopDetachReady = Promise.withResolvers<void>();
451453
desktopReleased = Promise.withResolvers<void>();
@@ -563,6 +565,33 @@ it.effect("a tab the desktop does not draw is marked headless, so the desktop st
563565
).pipe(Effect.provide(layer)),
564566
);
565567

568+
it.effect("a desktop page that loaded before the server attached reports its loaded title", () =>
569+
Effect.scoped(
570+
Effect.gen(function* () {
571+
const { broker } = yield* ready;
572+
const manager = yield* Manager.PreviewManager;
573+
const events = yield* manager.subscribeEvents;
574+
desktopRendersNext = true;
575+
desktopPageUrl = "https://example.com/loaded";
576+
const opened = yield* broker.invoke<PreviewAutomationStatus>({
577+
scope,
578+
operation: "open",
579+
input: { url: desktopPageUrl, reuseExistingTab: false, show: false },
580+
});
581+
let event = yield* PubSub.take(events);
582+
while (event.type !== "navigated" || event.tabId !== opened.tabId) {
583+
event = yield* PubSub.take(events);
584+
}
585+
586+
expect(event.snapshot.navStatus).toEqual({
587+
_tag: "Success",
588+
url: "https://example.com/loaded",
589+
title: "test page",
590+
});
591+
}),
592+
).pipe(Effect.provide(layer)),
593+
);
594+
566595
it.live.each([
567596
{ method: "goto" as const, message: { type: "navigate", url: "http://10.255.255.1/" } },
568597
{ method: "goBack" as const, message: { type: "history", delta: -1 } },

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,8 @@ const make = Effect.gen(function* () {
989989
page.on("crash", () => dropTab(tab, true));
990990
tabs.set(key, tab);
991991
reportLiveTabs();
992+
// A desktop page navigates on its own and can finish loading before these listeners attach.
993+
if (desktop) void page.waitForLoadState("load").then(() => reportLoaded(tab), constVoid);
992994
// Existing desktop and popup PDFs can start loading before interception attaches.
993995
if (
994996
snapshot.navStatus._tag === "Loading" &&

0 commit comments

Comments
 (0)