fix(documents): load the local PDF parser lazily so the vault routes stop crashing

The local content-extraction reader imported pdf-parse at module top level.
pdf-parse pulls in pdfjs, whose module evaluation references the browser-only
DOMMatrix global; on the server that reference throws during chunk
instantiation and takes down every route sharing the chunk — the document
list, upload, and search among them, so old and new documents alike fail to
load. Move the parser to a lazy import() inside the extraction function
(surface type kept as a type-only import). Verified under plain Node that the
lazy path still extracts a real PDF text layer, so local indexing is
unaffected.
This commit is contained in:
Marc-André Bombeck committed 2026-07-09 09:29:35 +02:00
1 parent 403cdf8cab
commit 951db4947e
5 files changed
+26 -4

No files matched your search

+13 -1
View File
@@ -20,7 +20,16 @@
*/
import { Buffer } from "node:buffer";
import { PDFParse } from "pdf-parse";
// `pdf-parse` pulls in pdfjs, whose module top-level references the browser-only
// `DOMMatrix` global. A STATIC import makes the Turbopack server chunk evaluate
// that reference at instantiation and throw `DOMMatrix is not defined`, taking
// down every route/worker that shares the chunk (the document routes among
// them). The value is therefore loaded lazily inside `extractPdfText` via a
// runtime `import()`, which lets pdf-parse initialise pdfjs on its own terms and
// never touches DOMMatrix at load — verified to extract a real text layer under
// plain Node. Keep the surface type as a type-only import (fully erased at
// build, no runtime module evaluation).
import type { PDFParse } from "pdf-parse";
import { annotate } from "@/lib/logging/context";
@@ -61,6 +70,9 @@ export async function extractPdfText(
): Promise<LocalExtractResult> {
let parser: PDFParse | null = null;
try {
// Lazy load: keeps pdfjs/DOMMatrix out of the eager server chunk (see the
// import-site note). Resolved at first PDF extraction, then module-cached.
const { PDFParse } = await import("pdf-parse");
parser = new PDFParse({ data: new Uint8Array(buffer) });
const result = await parser.getText({
// Bound the page walk and drop the default "-- N of M --" page markers so