The best way to control Claude Code, Codex and more from any computer or phone
fix(web): open PDF attachments no longer reload while the agent works (#39815)
Julius Marminge committed
Nov 20, 2026 at 09:24 UTC
f60c7475681ed1343c828d2c6a0d337df64a2152
2 files changed
+43
-14
apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx
+30
-1
@@ -20,9 +20,16 @@ vi.mock(import("pdfjs-dist/build/pdf.worker.min.mjs?url"), () => ({
20
default: pdfWorkerUrl,
21
}));
22
23
+// Like the real hook, the callback is a new function whenever its resource object is.
24
+const refreshers = new WeakMap<object, () => Promise<string | null>>();
25
vi.mock(import("~/assets/assetUrls"), async (original) => ({
26
...(await original()),
25
- useAssetUrlRefresh: () => refresh,
27
+ useAssetUrlRefresh: (_environmentId, resource) => {
28
+ if (!resource) return refresh;
29
+ let callback = refreshers.get(resource);
30
+ if (!callback) refreshers.set(resource, (callback = () => refresh()));
31
+ return callback;
32
+ },
33
useAssetUrlState: assetUrlState,
34
}));
35
// Hands each document pdf.js opens to the PDF test, which awaits its load instead of a poll window.
@@ -1232,6 +1239,28 @@ describe("document output sanitization", () => {
1239
expect(html).not.toContain("https://");
1240
});
1241
});
1242
+it("keeps an attachment URL when the parent passes a fresh asset object for the same attachment", async () => {
1243
+ const url = URL.createObjectURL(new Blob(["%PDF-1.4"], { type: "application/pdf" }));
1244
+ refresh.mockReset().mockResolvedValue(url);
1245
+ const preview = () => (
1246
+ <AttachmentFilePreview
1247
+ name="report.pdf"
1248
+ mimeType="application/pdf"
1249
+ sizeBytes={8}
1250
+ asset={{ environmentId: EnvironmentId.make("test"), attachmentId: "report" }}
1251
+ />
1252
+ );
1253
+ try {
1254
+ await using screen = await render(preview());
1255
+ await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(1));
1256
+ // FilePreviewPanel rebuilds `asset` when an agent command completes.
1257
+ await screen.rerender(preview());
1258
+ expect(refresh).toHaveBeenCalledTimes(1);
1259
+ } finally {
1260
+ URL.revokeObjectURL(url);
1261
+ }
1262
+});
1263
+
1264
it("opens a known empty attachment even when its range response is inaccessible", async () => {
1265
refresh.mockResolvedValue("https://unreachable.invalid/empty.txt");
1266
await using screen = await render(
apps/web/src/components/files/AttachmentFilePreview.tsx
+13
-13
@@ -134,19 +134,19 @@ export function AttachmentFilePreview(props: {
134
const { copyToClipboard, isCopied } = useCopyToClipboard({
135
target: namedClipboardTarget(props.name),
136
});
137
- // useAssetUrlRefresh keys its callback on these objects, so they must stay cached.
138
- const resource =
139
- props.asset && "resource" in props.asset
140
- ? props.asset.resource
141
- : attachmentId
142
- ? {
143
- _tag: "attachment" as const,
144
- attachmentId,
145
- fileName: props.name,
146
- mimeType: props.mimeType,
147
- disposition: "inline" as const,
148
- }
149
- : null;
137
+ // useAssetUrlRefresh keys its callback on these objects, so they must stay cached. Parents
138
+ // rebuild `asset` on every render, so the attachment resource derives only from its fields.
139
+ const assetResource = props.asset && "resource" in props.asset ? props.asset.resource : null;
140
+ const attachmentResource = attachmentId
141
+ ? {
142
+ _tag: "attachment" as const,
143
+ attachmentId,
144
+ fileName: props.name,
145
+ mimeType: props.mimeType,
146
+ disposition: "inline" as const,
147
+ }
148
+ : null;
149
+ const resource = assetResource ?? attachmentResource;
150
const downloadResource = resource
151
? resource._tag === "draft-workspace-file"
152
? resource