fix(invoice-inbox): ship pdfjs worker file to Vercel (#408)

* fix(invoice-inbox): include pdfjs worker file in function bundle

After #407 landed and the DOMMatrix error was gone, the next pdfjs failure
surfaced in prod logs:

  Setting up fake worker failed: "Cannot find module
  '/var/task/node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs'..."

pdfjs loads its worker module via dynamic import at runtime, which Next's
tracer can't see. With pdfjs-dist marked as a server external package the
main pdf.mjs ships but the worker file gets pruned. Force-include it via
outputFileTracingIncludes for any /api/extensions/ext/** route, since
that's where invoice-inbox dispatches.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor: narrow pdfjs worker tracing to invoice-inbox sub-path

Per Greptile review on #408. Other extensions don't use pdfjs and
shouldn't pay the ~1 MB worker cost. In practice all extension routes
share one catch-all bundle, so this is more about clarity of intent than
bundle size today, but future-proofs against per-segment bundling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-05-07 11:13:23 +02:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 5f9f72959f
commit 3a74f0ab61
+9 -1
View File
@@ -23,8 +23,16 @@ const nextConfig: NextConfig = {
// Keep pdfjs-dist out of the bundle. The legacy build references
// @napi-rs/canvas at module load and breaks Vercel's bundling step.
// Text extraction works in pure Node once DOM globals are stubbed at
// the call site (see extensions/general/invoice-inbox/lib/extract-invoice-fields.ts).
// module load (see extensions/general/invoice-inbox/lib/extract-invoice-fields.ts).
serverExternalPackages: ['pdfjs-dist'],
// Force the pdfjs worker file into the function bundle. Next.js's tracer
// can't see it (loaded dynamically by name), so without this it's missing
// in /var/task and getDocument() fails with "Setting up fake worker failed".
// Scoped to the invoice-inbox sub-path — other extensions don't use pdfjs
// and shouldn't pay the ~1 MB worker cost.
outputFileTracingIncludes: {
'/api/extensions/ext/invoice-inbox/**': ['./node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs'],
},
async redirects() {
return [
{