Skip to content

Commit 0f13d63

Browse files
fix(browser): Stop cancels loading pages (#29475)
Co-authored-by: Julius Marminge <[email protected]>
1 parent a4a8c61 commit 0f13d63

14 files changed

Lines changed: 266 additions & 14 deletions

File tree

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const PREVIEW_REGISTER_WEBVIEW_CHANNEL = "desktop:preview-register-webvie
8888
export const PREVIEW_NAVIGATE_CHANNEL = "desktop:preview-navigate";
8989
export const PREVIEW_GO_BACK_CHANNEL = "desktop:preview-go-back";
9090
export const PREVIEW_GO_FORWARD_CHANNEL = "desktop:preview-go-forward";
91+
export const PREVIEW_STOP_LOADING_CHANNEL = "desktop:preview-stop-loading";
9192
export const PREVIEW_REFRESH_CHANNEL = "desktop:preview-refresh";
9293
export const PREVIEW_ZOOM_IN_CHANNEL = "desktop:preview-zoom-in";
9394
export const PREVIEW_ZOOM_OUT_CHANNEL = "desktop:preview-zoom-out";

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ const refresh = tabMethod(
168168
"desktop.ipc.preview.refresh",
169169
(manager, tabId) => manager.refresh(tabId),
170170
);
171+
const stopLoading = tabMethod(
172+
IpcChannels.PREVIEW_STOP_LOADING_CHANNEL,
173+
"desktop.ipc.preview.stopLoading",
174+
(manager, tabId) => manager.stopLoading(tabId),
175+
);
171176
const zoomIn = tabMethod(
172177
IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
173178
"desktop.ipc.preview.zoomIn",
@@ -472,6 +477,7 @@ export const methods = [
472477
goBack,
473478
goForward,
474479
refresh,
480+
stopLoading,
475481
zoomIn,
476482
zoomOut,
477483
resetZoom,

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
389389
goBack: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_GO_BACK_CHANNEL, { tabId }),
390390
goForward: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_GO_FORWARD_CHANNEL, { tabId }),
391391
refresh: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_REFRESH_CHANNEL, { tabId }),
392+
stopLoading: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_STOP_LOADING_CHANNEL, { tabId }),
392393
zoomIn: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_ZOOM_IN_CHANNEL, { tabId }),
393394
zoomOut: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL, { tabId }),
394395
resetZoom: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL, { tabId }),

‎apps/desktop/src/preview/GuestInput.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ describe("preview guest input", () => {
4444
expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
4545
});
4646

47+
it("stops the guest navigation without stopping its embedder", async () => {
48+
let guestLoading = true;
49+
let hostLoading = true;
50+
const guest = {
51+
stop: () => {
52+
guestLoading = false;
53+
},
54+
};
55+
const debuggee = {
56+
sendCommand: async () => {
57+
hostLoading = false;
58+
return {};
59+
},
60+
};
61+
await sendPageCommand(
62+
guest as unknown as Electron.WebContents,
63+
debuggee as unknown as Electron.Debugger,
64+
"Page.stopLoading",
65+
{},
66+
);
67+
expect(guestLoading).toBe(false);
68+
expect(hostLoading).toBe(true);
69+
});
70+
4771
it.each([
4872
{
4973
sessionId: undefined,

‎apps/desktop/src/preview/GuestInput.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ export const sendPageCommand = async (
185185
else webContents.reload();
186186
return {};
187187
}
188+
if (method === "Page.stopLoading") {
189+
webContents.stop();
190+
return {};
191+
}
188192
if (!FOCUS_ROUTED_INPUT.has(method) || webContents.focusedFrame) {
189193
return debuggee.sendCommand(method, params);
190194
}

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21482148
if (wc.navigationHistory.canGoForward()) wc.navigationHistory.goForward();
21492149
});
21502150
const refresh = (tabId: string) => withWebContents("refresh", tabId, (wc) => wc.reload());
2151+
const stopLoading = (tabId: string) => withWebContents("stopLoading", tabId, (wc) => wc.stop());
21512152
const hardReload = (tabId: string) =>
21522153
withWebContents("hardReload", tabId, (wc) => wc.reloadIgnoringCache());
21532154
const findInPage = (
@@ -3531,6 +3532,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
35313532
prepareWebview,
35323533
reapplyZoom,
35333534
refresh,
3535+
stopLoading,
35343536
registerWebview,
35353537
resetZoom: (tabId: string) => applyZoom(tabId, () => DEFAULT_ZOOM_FACTOR),
35363538
setZoomFactor: (tabId: string, zoomFactor: number) =>
@@ -3732,6 +3734,7 @@ export class PreviewManager extends Context.Service<
37323734
readonly goBack: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37333735
readonly goForward: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37343736
readonly refresh: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
3737+
readonly stopLoading: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37353738
readonly zoomIn: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37363739
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
37373740
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
@@ -3906,6 +3909,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
39063909
goBack: operations.goBack,
39073910
goForward: operations.goForward,
39083911
refresh: operations.refresh,
3912+
stopLoading: operations.stopLoading,
39093913
zoomIn: operations.zoomIn,
39103914
zoomOut: operations.zoomOut,
39113915
resetZoom: operations.resetZoom,

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

Lines changed: 164 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
213213
viewport = size;
214214
}),
215215
goto: vi.fn(async (next: string, _options?: { timeout?: number; waitUntil?: string }) => {
216-
url = next;
217-
events.emit("load");
218216
const pending = initialNavigation;
219217
initialNavigation = null;
220218
await pending?.();
219+
url = next;
220+
events.emit("load");
221221
}),
222222
goBack: vi.fn(async () => {}),
223223
goForward: vi.fn(async () => {}),
@@ -998,15 +998,117 @@ it.effect("a desktop page that loaded before the server attached reports its loa
998998
).pipe(Effect.provide(layer)),
999999
);
10001000

1001+
it.effect.each(["none", "during", "after-fragment", "same-url"])(
1002+
"Stop settles an initial navigation with its request event $0",
1003+
(requestTiming) =>
1004+
Effect.scoped(
1005+
Effect.gen(function* () {
1006+
const { browser, broker } = yield* ready;
1007+
const manager = yield* Manager.PreviewManager;
1008+
const events = yield* manager.subscribeEvents;
1009+
const started = Promise.withResolvers<void>();
1010+
const committed = Promise.withResolvers<void>();
1011+
initialNavigation = () => {
1012+
started.resolve();
1013+
return committed.promise;
1014+
};
1015+
yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
1016+
const { tabId } = yield* manager.open({
1017+
threadId: scope.thread.threadId,
1018+
runtime: "server",
1019+
url:
1020+
requestTiming === "after-fragment"
1021+
? "https://example.com/pending#section"
1022+
: "https://example.com/pending",
1023+
});
1024+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1025+
yield* Effect.promise(() => started.promise);
1026+
const page = contexts.at(-1)!.page;
1027+
const request = {
1028+
url: () => "https://example.com/pending",
1029+
method: () => "GET",
1030+
isNavigationRequest: () => true,
1031+
frame: () => page,
1032+
failure: () => ({ errorText: "net::ERR_ABORTED" }),
1033+
};
1034+
const cdp = contexts.at(-1)!.sessions.at(-1)!;
1035+
const send = cdp.send.getMockImplementation()!;
1036+
cdp.send.mockImplementation(async (operation, input) => {
1037+
if (operation === "Page.stopLoading") {
1038+
committed.reject(new Error("net::ERR_ABORTED"));
1039+
if (requestTiming === "during") page.emit("request", request);
1040+
}
1041+
return send(operation, input);
1042+
});
1043+
yield* viewer.input({ type: "stopLoading" });
1044+
const stopped = yield* broker.invoke<PreviewAutomationStatus>({
1045+
scope,
1046+
tabId,
1047+
operation: "status",
1048+
input: {},
1049+
});
1050+
expect(stopped.loading).toBe(false);
1051+
let settled = yield* PubSub.take(events);
1052+
while (
1053+
settled.type !== "navigated" ||
1054+
settled.tabId !== tabId ||
1055+
settled.snapshot.navStatus._tag !== "Idle"
1056+
) {
1057+
settled = yield* PubSub.take(events);
1058+
}
1059+
const corrected = requestTiming === "same-url";
1060+
using timeout = vi.spyOn(globalThis, "setTimeout");
1061+
if (corrected) {
1062+
page.goto.mockImplementationOnce(async () => {
1063+
page.emit("request", request);
1064+
});
1065+
yield* viewer.input({ type: "navigate", url: request.url() });
1066+
} else if (requestTiming !== "none") {
1067+
if (requestTiming === "after-fragment") page.emit("request", request);
1068+
page.emit("requestfailed", request);
1069+
}
1070+
const status = yield* broker.invoke<PreviewAutomationStatus>({
1071+
scope,
1072+
tabId,
1073+
operation: "status",
1074+
input: {},
1075+
});
1076+
expect(status.loading).toBe(corrected);
1077+
expect(timeout).not.toHaveBeenCalledWith(expect.any(Function), 5_000);
1078+
if (!corrected) {
1079+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
1080+
expect(sessions.find((session) => session.tabId === tabId)?.navStatus).toEqual({
1081+
_tag: "Idle",
1082+
});
1083+
}
1084+
}),
1085+
).pipe(Effect.provide(layer)),
1086+
);
1087+
10011088
it.effect.each([
1002-
{ method: "goto" as const, message: { type: "navigate", url: "http://10.255.255.1/" } },
1003-
{ method: "goBack" as const, message: { type: "history", delta: -1 } },
1004-
{ method: "goForward" as const, message: { type: "history", delta: 1 } },
1005-
{ method: "reload" as const, message: { type: "reload" } },
1006-
])("a pending viewer $method allows a corrected URL and text input", ({ method, message }) =>
1089+
{
1090+
method: "goto" as const,
1091+
message: { type: "navigate", url: "http://10.255.255.1/" },
1092+
abortEvent: "before",
1093+
},
1094+
{ method: "goBack" as const, message: { type: "history", delta: -1 }, abortEvent: "before" },
1095+
{ method: "goForward" as const, message: { type: "history", delta: 1 }, abortEvent: "before" },
1096+
{ method: "reload" as const, message: { type: "reload" }, abortEvent: "after" },
1097+
{ method: "reload" as const, message: { type: "reload" }, abortEvent: "none" },
1098+
...["before", "after", "none"].map((abortEvent) => ({
1099+
method: "goto" as const,
1100+
message: { type: "navigate", url: "http://10.255.255.1/" },
1101+
abortEvent,
1102+
blank: true,
1103+
})),
1104+
])("viewer $method Stop settles ($abortEvent, blank=$blank)", (testCase) =>
10071105
Effect.scoped(
10081106
Effect.gen(function* () {
1009-
const { browser, tabId } = yield* ready;
1107+
const { method, message, abortEvent } = testCase;
1108+
const blank = "blank" in testCase;
1109+
const { browser, broker, tabId } = yield* ready;
1110+
const manager = yield* Manager.PreviewManager;
1111+
const events = yield* manager.subscribeEvents;
10101112
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
10111113
yield* viewer.input({ type: "takeControl" });
10121114
const started = Promise.withResolvers<void>();
@@ -1020,26 +1122,77 @@ it.effect.each([
10201122
yield* Effect.promise(() => started.promise);
10211123
const cdp = contexts[0]!.sessions.at(-1)!;
10221124
const send = cdp.send.getMockImplementation()!;
1125+
const request = {
1126+
url: () => "https://example.com/pending",
1127+
method: () => "GET",
1128+
isNavigationRequest: () => true,
1129+
frame: () => page,
1130+
failure: () => ({ errorText: "net::ERR_ABORTED" }),
1131+
};
10231132
const typed = Promise.withResolvers<void>();
1133+
let cancelled = false;
10241134
cdp.send.mockImplementation(async (operation, input) => {
10251135
if (operation === "Input.insertText") typed.resolve();
1136+
if (operation === "Page.stopLoading") {
1137+
cancelled = true;
1138+
committed.reject(new Error("net::ERR_ABORTED"));
1139+
if (abortEvent === "before") page.emit("requestfailed", request);
1140+
}
10261141
return send(operation, input);
10271142
});
10281143
const correction = yield* Effect.gen(function* () {
1029-
yield* viewer.input({ type: "navigate", url: "http://localhost:5173/fixed" });
1144+
if (!blank) yield* viewer.input({ type: "navigate", url: "http://localhost:5173/fixed" });
10301145
yield* viewer.input({ type: "text", text: "hello" });
10311146
}).pipe(Effect.forkScoped);
10321147
yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
10331148
// The text reaches the page while the first navigation is still uncommitted.
10341149
yield* Effect.promise(() => typed.promise);
10351150
expect(page.goto).toHaveBeenLastCalledWith(
1036-
"http://localhost:5173/fixed",
1151+
blank ? "http://10.255.255.1/" : "http://localhost:5173/fixed",
10371152
expect.objectContaining({ waitUntil: "commit" }),
10381153
);
10391154
expect(contexts[0]!.sessions.at(-1)!.send).toHaveBeenCalledWith("Input.insertText", {
10401155
text: "hello",
10411156
});
1042-
committed.resolve();
1157+
page.emit("request", request);
1158+
let loadingEvent = yield* PubSub.take(events);
1159+
while (
1160+
loadingEvent.type !== "navigated" ||
1161+
loadingEvent.snapshot.navStatus._tag !== "Loading"
1162+
) {
1163+
loadingEvent = yield* PubSub.take(events);
1164+
}
1165+
const reader = yield* browser.attachViewer(viewerInput(tabId, false));
1166+
yield* reader.input({ type: "stopLoading" });
1167+
expect(cancelled).toBe(false);
1168+
if (abortEvent === "none" && !blank) committed.resolve();
1169+
yield* viewer.input({ type: "stopLoading" });
1170+
expect(cancelled).toBe(true);
1171+
using timeout = vi.spyOn(globalThis, "setTimeout");
1172+
if (abortEvent === "after") page.emit("requestfailed", request);
1173+
expect(timeout).not.toHaveBeenCalledWith(expect.any(Function), 5_000);
1174+
const status = yield* broker.invoke<PreviewAutomationStatus>({
1175+
scope,
1176+
tabId,
1177+
operation: "status",
1178+
input: {},
1179+
});
1180+
expect(status.loading).toBe(false);
1181+
const expectedStatus = blank ? "Idle" : "Success";
1182+
let settled = yield* PubSub.take(events);
1183+
while (settled.type !== "navigated" || settled.snapshot.navStatus._tag !== expectedStatus) {
1184+
settled = yield* PubSub.take(events);
1185+
}
1186+
if (blank) {
1187+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
1188+
expect(sessions.find((session) => session.tabId === tabId)?.navStatus).toEqual({
1189+
_tag: "Idle",
1190+
});
1191+
}
1192+
if (settled.snapshot.navStatus._tag === "Success")
1193+
expect(settled.snapshot.navStatus.url).toBe("http://localhost:5173/fixed");
1194+
expect(page[method]).toHaveBeenCalledTimes(method === "goto" && !blank ? 2 : 1);
1195+
yield* viewer.input({ type: "releaseControl" });
10431196
yield* Fiber.join(navigation);
10441197
yield* Fiber.join(correction);
10451198
}),

0 commit comments

Comments
 (0)