From 3a74f0ab6181fa233d0b2752b841a6b336cff12c Mon Sep 17 00:00:00 2001 From: Jakob Wennberg <149234542+jakobwennberg@users.noreply.github.com> Date: Thu, 7 May 2026 11:13:23 +0200 Subject: [PATCH] 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) * 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) --------- Co-authored-by: Claude Opus 4.7 (1M context) --- next.config.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/next.config.ts b/next.config.ts index 2b4b4b2e..82a02ba2 100644 --- a/next.config.ts +++ b/next.config.ts @@ -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 [ {