mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 22:47:28 +08:00
Merge pull request #1469 from wumajiehechuan-lab/fix/pdfjs-legacy-bundle
fix(web): 加载带 polyfill 的 PDF.js 构建,让旧版引擎也能渲染
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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"]) {
|
||||
|
||||
Reference in New Issue
Block a user