From 61bfe2660189c050342b2f395cf8d14169badee2 Mon Sep 17 00:00:00 2001 From: BigSimmo <87357024+BigSimmo@users.noreply.github.com> Date: Sat, 18 Jul 2026 03:26:23 +0800 Subject: [PATCH 1/2] perf: lazy load document PDF reader --- src/components/DocumentViewer.tsx | 30 ++++++++++++++++++- tests/document-viewer-pdf-reader-lazy.test.ts | 18 +++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/document-viewer-pdf-reader-lazy.test.ts diff --git a/src/components/DocumentViewer.tsx b/src/components/DocumentViewer.tsx index 2ac217e8c..658c27bdd 100644 --- a/src/components/DocumentViewer.tsx +++ b/src/components/DocumentViewer.tsx @@ -1,6 +1,7 @@ "use client"; import Link from "next/link"; +import dynamic from "next/dynamic"; import { useRouter } from "next/navigation"; import { CircleAlert, @@ -64,7 +65,6 @@ import { } from "@/components/ui-primitives"; import { BadgeCluster } from "@/components/clinical-dashboard/clinical-badge"; import { SignedImage } from "@/components/clinical-dashboard/signed-image"; -import { NativePdfEmbed, PdfCanvasViewer } from "@/components/document-viewer/pdf-canvas-viewer"; import { NonPdfSourcePreview } from "@/components/document-viewer/non-pdf-source-preview"; import { clearCachedSignedUrl, getCachedSignedUrl, setCachedSignedUrl } from "@/lib/signed-url-cache"; import { readLocalProjectIdentity, unsafeLocalProjectMessage } from "@/lib/local-project-identity"; @@ -104,6 +104,34 @@ import { import { buildDocumentSummaryBadges } from "@/lib/document-summary-badges"; import { documentSummaryQuestion } from "@/lib/answer-contract"; +// pdf-canvas-viewer is only needed after a source document has loaded and the +// user is viewing a PDF. Keeping it out of the document route's initial client +// chunk avoids parsing its reader controls for image, text, and download-only +// documents. pdf.js itself remains loaded on demand by that component. +const PdfCanvasViewer = dynamic( + () => import("@/components/document-viewer/pdf-canvas-viewer").then((module) => module.PdfCanvasViewer), + { + ssr: false, + loading: () => , + }, +); +const NativePdfEmbed = dynamic( + () => import("@/components/document-viewer/pdf-canvas-viewer").then((module) => module.NativePdfEmbed), + { ssr: false, loading: () => }, +); + +function PdfPreviewLoading() { + return ( +
+ Loading PDF reader… +
+ ); +} + type PageRow = { id: string; page_number: number; diff --git a/tests/document-viewer-pdf-reader-lazy.test.ts b/tests/document-viewer-pdf-reader-lazy.test.ts new file mode 100644 index 000000000..1ec67d1e6 --- /dev/null +++ b/tests/document-viewer-pdf-reader-lazy.test.ts @@ -0,0 +1,18 @@ +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const viewerSource = readFileSync( + fileURLToPath(new URL("../src/components/DocumentViewer.tsx", import.meta.url)), + "utf8", +); + +describe("DocumentViewer PDF reader loading", () => { + it("keeps the PDF reader out of the document route's initial client chunk", () => { + expect(viewerSource).not.toMatch( + /import\s*\{[^}]*\b(?:NativePdfEmbed|PdfCanvasViewer)\b[^}]*\}\s*from\s*["']@\/components\/document-viewer\/pdf-canvas-viewer["']/, + ); + expect(viewerSource).toContain('dynamic(\n () => import("@/components/document-viewer/pdf-canvas-viewer")'); + expect(viewerSource).toContain("loading: () => "); + }); +}); From 00421e6dbd1402b49669a154e67afbdee7ace6a0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Jul 2026 19:48:35 +0000 Subject: [PATCH 2/2] test: strengthen DocumentViewer lazy-load assertions for both pdf components --- tests/document-viewer-pdf-reader-lazy.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/document-viewer-pdf-reader-lazy.test.ts b/tests/document-viewer-pdf-reader-lazy.test.ts index 1ec67d1e6..ad1168e11 100644 --- a/tests/document-viewer-pdf-reader-lazy.test.ts +++ b/tests/document-viewer-pdf-reader-lazy.test.ts @@ -14,5 +14,7 @@ describe("DocumentViewer PDF reader loading", () => { ); expect(viewerSource).toContain('dynamic(\n () => import("@/components/document-viewer/pdf-canvas-viewer")'); expect(viewerSource).toContain("loading: () => "); + expect(viewerSource).toContain("module.PdfCanvasViewer"); + expect(viewerSource).toContain("module.NativePdfEmbed"); }); });