Skip to content

Commit a6a5471

Browse files
committed
fix(browser): retain Stop across a delayed initial request
1 parent 50f5829 commit a6a5471

2 files changed

Lines changed: 112 additions & 42 deletions

File tree

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

Lines changed: 88 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -654,47 +654,92 @@ it.effect("a desktop page that loaded before the server attached reports its loa
654654
).pipe(Effect.provide(layer)),
655655
);
656656

657-
it.live("Stop settles an initial navigation before its request event", () =>
658-
Effect.scoped(
659-
Effect.gen(function* () {
660-
const { browser } = yield* ready;
661-
const manager = yield* Manager.PreviewManager;
662-
const events = yield* manager.subscribeEvents;
663-
const started = Promise.withResolvers<void>();
664-
const committed = Promise.withResolvers<void>();
665-
initialNavigation = () => {
666-
started.resolve();
667-
return committed.promise;
668-
};
669-
yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
670-
const { tabId } = yield* manager.open({
671-
threadId: scope.thread.threadId,
672-
runtime: "server",
673-
url: "https://example.com/pending",
674-
});
675-
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
676-
yield* Effect.promise(() => started.promise);
677-
const cdp = contexts.at(-1)!.sessions.at(-1)!;
678-
const send = cdp.send.getMockImplementation()!;
679-
cdp.send.mockImplementation(async (operation, input) => {
680-
if (operation === "Page.stopLoading") committed.reject(new Error("net::ERR_ABORTED"));
681-
return send(operation, input);
682-
});
683-
yield* viewer.input({ type: "stopLoading" });
684-
let settled = yield* PubSub.take(events);
685-
while (
686-
settled.type !== "navigated" ||
687-
settled.tabId !== tabId ||
688-
settled.snapshot.navStatus._tag !== "Idle"
689-
) {
690-
settled = yield* PubSub.take(events);
691-
}
692-
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
693-
expect(sessions.find((session) => session.tabId === tabId)?.navStatus).toEqual({
694-
_tag: "Idle",
695-
});
696-
}),
697-
).pipe(Effect.provide(layer)),
657+
it.live.each(["none", "during", "after", "after-fragment", "corrected", "same-url"])(
658+
"Stop settles an initial navigation with its request event $0",
659+
(requestTiming) =>
660+
Effect.scoped(
661+
Effect.gen(function* () {
662+
const { browser, broker } = yield* ready;
663+
const manager = yield* Manager.PreviewManager;
664+
const events = yield* manager.subscribeEvents;
665+
const started = Promise.withResolvers<void>();
666+
const committed = Promise.withResolvers<void>();
667+
initialNavigation = () => {
668+
started.resolve();
669+
return committed.promise;
670+
};
671+
yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
672+
const { tabId } = yield* manager.open({
673+
threadId: scope.thread.threadId,
674+
runtime: "server",
675+
url:
676+
requestTiming === "after-fragment"
677+
? "https://example.com/pending#section"
678+
: "https://example.com/pending",
679+
});
680+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
681+
yield* Effect.promise(() => started.promise);
682+
const page = contexts.at(-1)!.page;
683+
const request = {
684+
url: () => "https://example.com/pending",
685+
method: () => "GET",
686+
isNavigationRequest: () => true,
687+
frame: () => page,
688+
failure: () => ({ errorText: "net::ERR_ABORTED" }),
689+
};
690+
const cdp = contexts.at(-1)!.sessions.at(-1)!;
691+
const send = cdp.send.getMockImplementation()!;
692+
cdp.send.mockImplementation(async (operation, input) => {
693+
if (operation === "Page.stopLoading") {
694+
committed.reject(new Error("net::ERR_ABORTED"));
695+
if (requestTiming === "during") page.emit("request", request);
696+
}
697+
return send(operation, input);
698+
});
699+
yield* viewer.input({ type: "stopLoading" });
700+
const stopped = yield* broker.invoke<PreviewAutomationStatus>({
701+
scope,
702+
tabId,
703+
operation: "status",
704+
input: {},
705+
});
706+
expect(stopped.loading).toBe(false);
707+
let settled = yield* PubSub.take(events);
708+
while (
709+
settled.type !== "navigated" ||
710+
settled.tabId !== tabId ||
711+
settled.snapshot.navStatus._tag !== "Idle"
712+
) {
713+
settled = yield* PubSub.take(events);
714+
}
715+
const corrected = requestTiming === "corrected" || requestTiming === "same-url";
716+
using timeout = vi.spyOn(globalThis, "setTimeout");
717+
if (corrected) {
718+
const url = requestTiming === "same-url" ? request.url() : "https://example.com/fixed";
719+
page.goto.mockImplementationOnce(async () => {
720+
page.emit("request", { ...request, url: () => url });
721+
});
722+
yield* viewer.input({ type: "navigate", url });
723+
} else if (requestTiming !== "none") {
724+
if (requestTiming.startsWith("after")) page.emit("request", request);
725+
page.emit("requestfailed", request);
726+
}
727+
const status = yield* broker.invoke<PreviewAutomationStatus>({
728+
scope,
729+
tabId,
730+
operation: "status",
731+
input: {},
732+
});
733+
expect(status.loading).toBe(corrected);
734+
expect(timeout).not.toHaveBeenCalledWith(expect.any(Function), 5_000);
735+
if (!corrected) {
736+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
737+
expect(sessions.find((session) => session.tabId === tabId)?.navStatus).toEqual({
738+
_tag: "Idle",
739+
});
740+
}
741+
}),
742+
).pipe(Effect.provide(layer)),
698743
);
699744

700745
it.live.each([
@@ -780,7 +825,9 @@ it.live.each([
780825
if (abortEvent === "none" && !blank) committed.resolve();
781826
yield* viewer.input({ type: "stopLoading" });
782827
expect(cancelled).toBe(true);
828+
using timeout = vi.spyOn(globalThis, "setTimeout");
783829
if (abortEvent === "after") page.emit("requestfailed", request);
830+
expect(timeout).not.toHaveBeenCalledWith(expect.any(Function), 5_000);
784831
const status = yield* broker.invoke<PreviewAutomationStatus>({
785832
scope,
786833
tabId,

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ interface ServerTab {
331331
closing: boolean;
332332
recording: Recording | null;
333333
initialNavigation: Promise<void> | null;
334+
pendingInitialRequestUrl: string | null;
334335
navigationGeneration: number;
335336
stoppedGeneration: number | null;
336337

@@ -892,6 +893,7 @@ const make = Effect.gen(function* () {
892893
recording: null,
893894
recordingStart: null,
894895
initialNavigation: null,
896+
pendingInitialRequestUrl: null,
895897
navigationGeneration: 0,
896898
stoppedGeneration: null,
897899
abortedNavigation: null,
@@ -922,16 +924,27 @@ const make = Effect.gen(function* () {
922924
const navigationGenerations = new WeakMap<object, number>();
923925
page.on("request", (request) => {
924926
if (!isMainNavigation(request)) return;
927+
// Playwright can deliver the first request after Stop has already settled its navigation.
928+
const stoppedInitialRequest =
929+
request.url() === tab.pendingInitialRequestUrl &&
930+
tab.stoppedGeneration === tab.navigationGeneration;
931+
tab.pendingInitialRequestUrl = null;
925932
navigationGenerations.set(request, ++tab.navigationGeneration);
926933
clearAbortedNavigation(tab);
927934
tab.loading = true;
935+
if (stoppedInitialRequest) {
936+
tab.stoppedGeneration = tab.navigationGeneration;
937+
settleStoppedNavigation(tab, tab.navigationGeneration);
938+
return;
939+
}
928940
report(tab, {
929941
_tag: "Loading",
930942
url: ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048),
931943
title: "",
932944
});
933945
});
934946
page.on("load", () => {
947+
tab.pendingInitialRequestUrl = null;
935948
tab.loading = false;
936949
void reportLoaded(tab);
937950
});
@@ -1034,6 +1047,9 @@ const make = Effect.gen(function* () {
10341047
((!adopted && !desktop) || isPdfAssetUrl(snapshot.navStatus.url))
10351048
) {
10361049
tab.loading = true;
1050+
const initialUrl = new URL(snapshot.navStatus.url);
1051+
initialUrl.hash = "";
1052+
tab.pendingInitialRequestUrl = initialUrl.href;
10371053
const options = { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS } as const;
10381054
tab.initialNavigation = (
10391055
(desktop || adopted) && page.url() === snapshot.navStatus.url
@@ -1693,6 +1709,7 @@ const make = Effect.gen(function* () {
16931709
readiness: "load" | "domContentLoaded" | "none",
16941710
timeout: number,
16951711
) => {
1712+
tab.pendingInitialRequestUrl = null;
16961713
const navigation = tab.page.goto(url, {
16971714
timeout,
16981715
waitUntil:
@@ -2369,7 +2386,10 @@ const make = Effect.gen(function* () {
23692386
const request = event.request;
23702387
if (request?.clear === "cookies") await tab.cdp.send("Network.clearBrowserCookies");
23712388
if (request?.clear === "cache") await tab.cdp.send("Network.clearBrowserCache");
2372-
if (request?.hardReload) await tab.cdp.send("Page.reload", { ignoreCache: true });
2389+
if (request?.hardReload) {
2390+
tab.pendingInitialRequestUrl = null;
2391+
await tab.cdp.send("Page.reload", { ignoreCache: true });
2392+
}
23732393
})
23742394
.catch((cause: unknown) =>
23752395
runFork(Effect.logWarning("server preview could not apply a tab setting", { cause })),
@@ -2503,10 +2523,12 @@ const make = Effect.gen(function* () {
25032523
case "navigate":
25042524
if (typeof message.url === "string") {
25052525
const url = normalizePreviewUrl(message.url);
2526+
tab.pendingInitialRequestUrl = null;
25062527
tab.control.trackUntilHandoff(tab.page.goto(url, VIEWER_NAVIGATION_OPTIONS));
25072528
}
25082529
return;
25092530
case "history":
2531+
tab.pendingInitialRequestUrl = null;
25102532
tab.control.trackUntilHandoff(
25112533
num(message.delta) < 0
25122534
? tab.page.goBack(VIEWER_NAVIGATION_OPTIONS)
@@ -2522,6 +2544,7 @@ const make = Effect.gen(function* () {
25222544
return;
25232545
}
25242546
case "reload":
2547+
tab.pendingInitialRequestUrl = null;
25252548
if (message.ignoreCache === true) {
25262549
await session.send("Page.reload", { ignoreCache: true });
25272550
return;

0 commit comments

Comments
 (0)