Skip to content

Commit e242d76

Browse files
AndresL230claude
andcommitted
fix(cf): lazy-load MarkdownChat to shrink worker bundle further
MarkdownChat statically imports remark-math, remark-gfm, remark-directive, rehype-katex, rehype-highlight, plus side-effect CSS for katex and highlight.js, and renders MermaidBlock/FunctionPlot. Even though the component is "use client", static-importing it from ChatPanel.tsx, screens/Library.tsx, and screens/Study.tsx pulled the whole markdown stack into the OpenNext server handler. Switch all three call sites to `dynamic(() => import(...), { ssr: false })` so MarkdownChat becomes a separate client chunk. Local cf:build verification: - handler.mjs uncompressed: 9,933 KiB → 5,041 KiB - handler.mjs gzipped: 2,577 KiB → 1,331 KiB (well under 3 MiB) - mermaid/katex/highlight.js refs in handler.mjs: 79 → 0 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8ae9fe8 commit e242d76

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

frontend/src/components/ChatPanel.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
"use client";
22

33
import React, { useEffect, useRef, useState } from "react";
4+
import dynamic from "next/dynamic";
45
import { Icon } from "./Icon";
5-
import { MarkdownChat } from "./MarkdownChat";
6+
7+
// MarkdownChat statically imports mermaid, katex, highlight.js, and the
8+
// remark/rehype stack. Static-importing it from a client component still
9+
// pulls those modules through next.config.ts → transpilePackages, which
10+
// bloats the OpenNext worker bundle on Cloudflare. Lazy-load with
11+
// ssr:false so the heavy markdown stack is a separate client chunk.
12+
const MarkdownChat = dynamic(
13+
() => import("./MarkdownChat").then((m) => m.MarkdownChat),
14+
{ ssr: false, loading: () => null },
15+
);
616

717
export type ChatRole = "user" | "assistant";
818
export interface ChatMsg {

frontend/src/components/screens/Library.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
"use client";
22
import React from "react";
3+
import dynamic from "next/dynamic";
34
import { createPortal } from "react-dom";
45
import { TopBar } from "../TopBar";
56
import { Icon } from "../Icon";
67
import { Pill } from "../Pill";
78
import { DocumentUploadModal } from "../DocumentUploadModal";
89
import { LibraryGridSkeleton, LibraryListSkeleton } from "../Skeleton";
9-
import { MarkdownChat } from "../MarkdownChat";
10+
11+
// Lazy-load to keep mermaid/katex/highlight.js out of the OpenNext
12+
// server bundle. See ChatPanel.tsx for the rationale.
13+
const MarkdownChat = dynamic(
14+
() => import("../MarkdownChat").then((m) => m.MarkdownChat),
15+
{ ssr: false, loading: () => null },
16+
);
1017
import { useToast } from "../ToastProvider";
1118
import { useConfirm } from "@/lib/useConfirm";
1219
import { useIsMobile } from "@/lib/useIsMobile";

frontend/src/components/screens/Study.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
"use client";
22
import React from "react";
3+
import dynamic from "next/dynamic";
34
import { useSearchParams } from "next/navigation";
45
import { AnimatePresence, motion } from "framer-motion";
56
import { TopBar } from "../TopBar";
67
import { AIDisclaimerChip } from "../AIDisclaimerChip";
78
import { Icon } from "../Icon";
89
import { Pill } from "../Pill";
910
import { CustomSelect } from "../CustomSelect";
10-
import { MarkdownChat } from "../MarkdownChat";
11+
12+
// Lazy-load to keep mermaid/katex/highlight.js out of the OpenNext
13+
// server bundle. See ChatPanel.tsx for the rationale.
14+
const MarkdownChat = dynamic(
15+
() => import("../MarkdownChat").then((m) => m.MarkdownChat),
16+
{ ssr: false, loading: () => null },
17+
);
1118
import { StudyGuideSkeleton, FlashcardsSkeleton } from "../Skeleton";
1219
import { useToast } from "../ToastProvider";
1320
import { useIsMobile } from "@/lib/useIsMobile";

0 commit comments

Comments
 (0)