From 172cae8aa2dad77f4ea05bd7f80bf1fba6c46601 Mon Sep 17 00:00:00 2001 From: wumajiehechuan-lab Date: Mon, 14 Sep 2026 20:25:39 -0700 Subject: [PATCH] fix(web): load the polyfilled PDF.js build so older engines can render pdfjs-dist ships two builds. The default one is neither transpiled nor polyfilled: it targets the newest engines and calls recent ECMAScript methods such as `Map.prototype.getOrInsertComputed` (ES2026; Chrome/Edge 145+, Firefox 144+) straight off the platform. On any slightly older engine -- including a browser or embedded webview only a few months behind -- the worker throws this._requestsByChunk.getOrInsertComputed is not a function and the document never renders, silently, because the failure happens inside the worker where the main thread cannot see it. Take both the library and its worker from the `legacy` bundle, which carries core-js and patches those methods in. The two must come from the same bundle, or PDF.js aborts with an API/Worker version mismatch. The extra weight is paid only by users who actually open a PDF, since this module loads dynamically. Pin the choice with a test so it is not "optimised" back. --- web/lib/pdfjs-loader.ts | 23 +++++++++++++++++++++-- web/tests/build-wrapper.test.ts | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/web/lib/pdfjs-loader.ts b/web/lib/pdfjs-loader.ts index af13618ee..807c01028 100644 --- a/web/lib/pdfjs-loader.ts +++ b/web/lib/pdfjs-loader.ts @@ -10,6 +10,22 @@ * hand-written public path, which lets the bundler fingerprint and serve the * worker file itself — a hard-coded `/pdf.worker.mjs` would have to be copied * into `public/` by a build step and would break the moment the version bumps. + * + * Both the library and its worker are taken from the `legacy` bundle on + * purpose. `pdfjs-dist` ships two builds: the default one is neither + * transpiled nor polyfilled — it targets the very latest engines and calls + * recent ECMAScript methods such as `Map.prototype.getOrInsertComputed` + * (ES2026; Chrome/Edge 145+, Firefox 144+) straight off the platform. In any + * older engine, including a browser or embedded webview that is merely some + * months behind, the worker dies with + * + * this._requestsByChunk.getOrInsertComputed is not a function + * + * and the document never renders — silently, since the failure happens in the + * worker. The `legacy` bundle carries core-js and patches those methods in. + * It costs roughly 100 KB more, paid only by users who actually open a PDF, + * because this module is imported dynamically. Do not "optimise" this back to + * the default build. */ import type * as PdfjsModule from "pdfjs-dist"; @@ -25,10 +41,13 @@ let pending: Promise | null = null; export function loadPdfjs(): Promise { if (pending) return pending; pending = (async () => { - const pdfjs = await import("pdfjs-dist"); + const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs"); if (!pdfjs.GlobalWorkerOptions.workerSrc) { + // Must stay on the same bundle as the import above: mixing the legacy + // library with the default worker (or vice versa) fails with + // `The API version "a.b.c" does not match the Worker version "x.y.z"`. pdfjs.GlobalWorkerOptions.workerSrc = new URL( - "pdfjs-dist/build/pdf.worker.min.mjs", + "pdfjs-dist/legacy/build/pdf.worker.min.mjs", import.meta.url, ).toString(); } diff --git a/web/tests/build-wrapper.test.ts b/web/tests/build-wrapper.test.ts index 6de22c8da..0715b6d9b 100644 --- a/web/tests/build-wrapper.test.ts +++ b/web/tests/build-wrapper.test.ts @@ -55,6 +55,22 @@ test("the reader gives PDF.js an absolute same-origin decoder URL", () => { assert.match(reader, /wasmUrl:\s*pdfjsWasmUrl\(\)/); }); +test("the reader loads the polyfilled PDF.js build, not the latest-engines one", () => { + const loader = read("lib", "pdfjs-loader.ts"); + // `pdfjs-dist`'s default bundle is neither transpiled nor polyfilled, and it + // calls `Map.prototype.getOrInsertComputed` (ES2026; Chrome/Edge 145+) + // directly. On anything older the worker throws + // `this._requestsByChunk.getOrInsertComputed is not a function` and the + // reader shows nothing — a failure invisible to a developer on a current + // browser, which is exactly why it is pinned here. The library and its + // worker must also come from the same bundle, or PDF.js aborts with a + // version-mismatch error. + assert.doesNotMatch(loader, /import\("pdfjs-dist"\)/); + assert.doesNotMatch(loader, /"pdfjs-dist\/build\//); + assert.match(loader, /import\("pdfjs-dist\/legacy\/build\/pdf\.mjs"\)/); + assert.match(loader, /"pdfjs-dist\/legacy\/build\/pdf\.worker\.min\.mjs"/); +}); + test("the build wrapper restores every generated checked-in input", () => { const source = read("scripts", "build.mjs"); for (const name of ["next-env.d.ts", "tsconfig.json"]) {