Skip to content

Commit d8a829e

Browse files
saphidclaude
andauthored
perf(web): PDF previews reload without starting a new pdf.js worker (#29626)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 57cd059 commit d8a829e

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,7 @@ it("keeps workspace PDF scroll and zoom after reloads", async () => {
25222522
const { preview, urls } = fixture;
25232523
await using screen = await render(preview(urls.original, "one"));
25242524
await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible();
2525+
using workers = vi.spyOn(window, "Worker");
25252526
await screen.getByRole("button", { name: "Next page", exact: true }).click();
25262527
await vi.waitFor(() => expect(screen.container.textContent).toContain("2 / 3 pages"));
25272528
await screen.getByRole("button", { name: "Next page", exact: true }).click();
@@ -2576,6 +2577,7 @@ it("keeps workspace PDF scroll and zoom after reloads", async () => {
25762577
expect(region().scrollTop).toBe(0);
25772578
expect(region().scrollLeft).toBe(0);
25782579
});
2580+
expect(workers).not.toHaveBeenCalled();
25792581
});
25802582

25812583
it("clamps workspace PDF scroll when pages shrink or disappear", async () => {

‎apps/web/src/components/files/pdfDocument.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDocument, GlobalWorkerOptions } from "pdfjs-dist";
1+
import { getDocument, GlobalWorkerOptions, PDFWorker } from "pdfjs-dist";
22
import workerUrl from "pdfjs-dist/build/pdf.worker.min.mjs?url";
33
import fallbackUrl from "pdfjs-dist/wasm/openjpeg_nowasm_fallback.js?url";
44

@@ -31,8 +31,13 @@ export class BinaryDataFactory {
3131
}
3232
}
3333

34+
// Without a shared worker, pdf.js starts a worker and recompiles its 1.3 MB script for every document.
35+
let worker: PDFWorker | undefined;
36+
3437
export function openPdf(bytes: ArrayBuffer) {
38+
worker ??= new PDFWorker();
3539
return getDocument({
40+
worker,
3641
data: new Uint8Array(bytes),
3742
useSystemFonts: true,
3843
useWorkerFetch: false,

0 commit comments

Comments
 (0)