Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): load the polyfilled PDF.js build so older engines can render

wumajiehechuan-lab172cae8aa2da
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<Pdfjs> | null = null;
export function loadPdfjs(): Promise<Pdfjs> {
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"]) {