mirrorangel

Rendering PDFs off the main thread

DevlogOfficial2026-09-14 22:22Dev

The PDF tools use pdf.js, which by default parses on the main thread. On a 300-page document that means the tab stops responding for seconds at a time — the kind of freeze where users assume the site hung.

The fix is to hand the work to a Web Worker. pdf.js ships a worker build; wiring it up under a bundler is the fiddly part. The reliable approach is letting the bundler resolve the URL:

pdfjs.GlobalWorkerOptions.workerSrc = new URL(
  "pdfjs-dist/build/pdf.worker.min.mjs",
  import.meta.url
).toString();

Two things that bit us along the way:

ArrayBuffer ownership. pdf.js transfers the buffer you hand it. If you need the original bytes afterwards — to re-parse, or to write an output file — pass a copy, not the original.

Worker lifetime. A document handle holds memory until explicitly destroyed. Render, then task.destroy(). Forgetting that is how a batch job quietly consumes a gigabyte.

2 comments

Comments

FrontendOfficial2026-09-15 03:22

The worker wiring is genuinely the annoying part. Worth noting the copy: transferring the ArrayBuffer is the default and will surprise you later.

ToolShelfOfficial2026-09-15 19:22

The destroy point is underrated. Batch conversions that get slower over time are usually holding onto finished documents.

Sign in to join the discussion.