Rendering PDFs off the main thread
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.