Skip to content

Commit 06e7145

Browse files
fcgithclaude
andcommitted
fix(server): adopted popups report their load once
A popup that was still loading when its tab was set up fired both the `load` listener and the setup-time load-state wait, so clients got two identical Success updates for one load. The wait now reports only when the listener has not seen a load. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1cc02d1 commit 06e7145

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,40 @@ it.live("an adopted popup that is still loading is not reported as successful ea
11591159
).pipe(Effect.provide(layer)),
11601160
);
11611161

1162+
it.live("an adopted popup that loads after setup reports success once", () =>
1163+
Effect.scoped(
1164+
Effect.gen(function* () {
1165+
const { browser, tabId } = yield* ready;
1166+
const manager = yield* Manager.PreviewManager;
1167+
const events = yield* manager.subscribeEvents;
1168+
const opener = contexts[0]!.page;
1169+
const popup = makeContext();
1170+
const url = "https://example.com/popup-loads-later";
1171+
const loadState = Promise.withResolvers<void>();
1172+
popup.setPageUrl(url);
1173+
popup.page.waitForLoadState.mockImplementationOnce(() => loadState.promise);
1174+
1175+
opener.emit("popup", popup.page);
1176+
let opened = yield* PubSub.take(events);
1177+
while (opened.type !== "opened" || opened.snapshot.tabId === tabId) {
1178+
opened = yield* PubSub.take(events);
1179+
}
1180+
yield* browser.attachViewer(viewerInput(opened.snapshot.tabId, false));
1181+
1182+
// Playwright settles the load-state wait and emits `load` for the same load.
1183+
loadState.resolve();
1184+
popup.page.emit("load");
1185+
expect(yield* nextLoaded(events, opened.snapshot.tabId)).toEqual({
1186+
_tag: "Success",
1187+
url,
1188+
title: "test page",
1189+
});
1190+
// Each report reads the title first, and a duplicate starts before the first one publishes.
1191+
expect(popup.page.title).toHaveBeenCalledTimes(1);
1192+
}),
1193+
).pipe(Effect.provide(layer)),
1194+
);
1195+
11621196
it.live("a tab opened to a file the browser cannot show reports the file to download", () =>
11631197
Effect.scoped(
11641198
Effect.gen(function* () {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,16 @@ const make = Effect.gen(function* () {
915915
void tab.initialNavigation.catch(constVoid);
916916
} else if (adopted) {
917917
// Popup navigation can finish during setup, before this tab's listeners exist.
918+
// A load after setup settles this wait too, and the `load` listener reports that one.
919+
let loadReported = false;
920+
page.once("load", () => {
921+
loadReported = true;
922+
});
918923
void page
919924
.waitForLoadState("load")
920-
.then(() => reportLoaded(tab))
925+
.then(() => {
926+
if (!loadReported) return reportLoaded(tab);
927+
})
921928
.catch(constVoid);
922929
}
923930
return tab;

0 commit comments

Comments
 (0)