fix(pdf): self-host worker as .js (prod .mjs MIME breaks module import)#101
Merged
Conversation
The pdf.js worker was emitted as a hashed `.mjs` asset via `?url`. Prod
nginx has no `.mjs` MIME mapping, so it served the file as
`application/octet-stream`, and the browser refused to import it as a
module — "Setting up fake worker failed: Failed to fetch dynamically
imported module …pdf.worker.min-*.mjs".
Copy the worker into `public/pdfjs/pdf.worker.min.js` (alongside the
cmaps/fonts/wasm already served from there) and point
`GlobalWorkerOptions.workerSrc` at that static `.js` path. pdf.js always
spawns the worker with `{type:"module"}` regardless of extension, and
nginx serves `.js` as `application/javascript`, so the module loads.
No server change required.
Verified against `pnpm preview`: worker served as text/javascript, no
`.mjs` asset emitted, 3-page CJK PDF renders identically, no console
errors.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XBhzzooAQNwaM4vg1CmpFo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On production, opening a PDF fails with:
Root cause: the pdf.js worker was emitted as a hashed
.mjsasset. Prod nginx has no.mjsMIME mapping, so it serves the file asapplication/octet-stream— and browsers refuse toimport()an octet-stream as a module. Confirmed on prod:…/pdf.worker.min-*.mjsapplication/octet-stream❌…/index-*.jsapplication/javascript✅…/openjpeg.wasmapplication/wasm✅Only the
.mjsextension falls through.Fix (repo-only, no server change)
Copy the worker into
public/pdfjs/pdf.worker.min.js(alongside the cmaps/fonts/wasm already self-hosted there) and pointGlobalWorkerOptions.workerSrcat that static.jspath. pdf.js always spawns the worker with{type:"module"}regardless of file extension, and nginx serves.jsasapplication/javascript, so the module worker loads. No.mjsasset is emitted anymore.Verification
pnpm typecheck/lint/buildgreen;pnpm test393 passing.pnpm preview: worker served astext/javascript, no.mjsindist/, 3-page CJK PDF renders identically (correct dims, glyphs, white JPEG matte), zero console errors.Note
The underlying nginx
.mjsgap still exists and could bite any future.mjs. Optional hardening (server-side, not in this PR): map.mjs → text/javascriptin nginx. This PR makes the app not depend on it.🤖 Generated with Claude Code
https://claude.ai/code/session_01XBhzzooAQNwaM4vg1CmpFo