Skip to content

Commit b15b115

Browse files
committed
fix(server): preserve downloads from initial preview navigation
1 parent dc23e27 commit b15b115

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,50 @@ it.live("preview_open reports its navigation timeout instead of the request budg
514514
).pipe(Effect.provide(layer)),
515515
);
516516

517+
it.live("an initial download stays available after preview_open reports the navigation error", () =>
518+
Effect.scoped(
519+
Effect.gen(function* () {
520+
const { broker, tabId } = yield* ready;
521+
const manager = yield* Manager.PreviewManager;
522+
const released = Promise.withResolvers<void>();
523+
initialNavigation = async () => {
524+
contexts[1]!.page.emit("download", {
525+
failure: async () => null,
526+
delete: async () => released.resolve(),
527+
saveAs: (path: string) => NodeFSP.writeFile(path, "a,b"),
528+
suggestedFilename: () => "report.csv",
529+
url: () => "http://localhost:1337/report.csv",
530+
});
531+
throw new Error("page.goto: Download is starting");
532+
};
533+
const error = yield* broker
534+
.invoke<PreviewAutomationStatus>({
535+
scope,
536+
operation: "open",
537+
input: { url: "http://localhost:1337/report.csv", reuseExistingTab: false, open: false },
538+
})
539+
.pipe(Effect.flip);
540+
expect(error.message).toContain("Download is starting");
541+
yield* Effect.promise(() => released.promise);
542+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
543+
expect(sessions).toHaveLength(2);
544+
const downloadTabId = PreviewTabId.make(sessions.find((tab) => tab.tabId !== tabId)!.tabId);
545+
const status = yield* broker.invoke<PreviewAutomationStatus>({
546+
scope,
547+
tabId: downloadTabId,
548+
operation: "status",
549+
input: {},
550+
});
551+
expect(status.downloads).toEqual([
552+
expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
553+
]);
554+
expect(yield* (yield* FileSystem.FileSystem).readFileString(status.downloads![0]!.path)).toBe(
555+
"a,b",
556+
);
557+
}),
558+
).pipe(Effect.provide(layer)),
559+
);
560+
517561
it.live.each(["desktop" as const, undefined])(
518562
"explicit desktop targets ($0 runtime) fail without creating a server tab",
519563
(runtime) =>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,10 @@ const make = Effect.gen(function* () {
20112011
} else {
20122012
// A failed open returns no tabId; a leftover hidden tab would block later reuse.
20132013
await tab.initialNavigation?.catch(async (cause: unknown) => {
2014+
// Playwright rejects downloads too; their tab must survive to save the file.
2015+
if (cause instanceof Error && cause.message.includes("Download is starting")) {
2016+
throw cause;
2017+
}
20142018
await runPromise(manager.close({ threadId: tab.threadId, tabId: tab.tabId }));
20152019
dropTab(tab, false);
20162020
throw cause;

0 commit comments

Comments
 (0)