Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

perf: lazy-load KaTeX via dynamic import of @streamdown/math (#8874)

Myles Scolnick65860a2b75da
diff --git a/frontend/src/components/markdown/markdown-renderer.tsx b/frontend/src/components/markdown/markdown-renderer.tsx
index 5f621e5eb2..05ccc31c26 100644
--- a/frontend/src/components/markdown/markdown-renderer.tsx
+++ b/frontend/src/components/markdown/markdown-renderer.tsx
@@ -1,7 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { EditorView } from "@codemirror/view";
-import { math } from "@streamdown/math";
import { useAtomValue } from "jotai";
import { BetweenHorizontalStartIcon } from "lucide-react";
import { memo, Suspense, useState } from "react";
@@ -13,6 +12,7 @@ import { useLastFocusedCellId } from "@/core/cells/focus";
import { MarkdownLanguageAdapter } from "@/core/codemirror/language/languages/markdown";
import { SQLLanguageAdapter } from "@/core/codemirror/language/languages/sql/sql";
import { autoInstantiateAtom } from "@/core/config/config";
+import { useAsyncData } from "@/hooks/useAsyncData";
import { LazyAnyLanguageCodeMirror } from "@/plugins/impl/code/LazyAnyLanguageCodeMirror";
import { useTheme } from "@/theme/useTheme";
import { copyToClipboard } from "@/utils/copy";
@@ -166,11 +166,27 @@ const COMPONENTS: Components = {
},
};
+// Lazy-load the math plugin to keep katex (~264 KB) out of the critical path.
+// The first render works without math; once loaded, it re-renders with math support.
+let mathPluginCache: StreamdownProps["plugins"] | undefined;
+const useMathPlugin = () => {
+ const { data: plugins } = useAsyncData(async () => {
+ if (mathPluginCache) {
+ return mathPluginCache;
+ }
+ const mod = await import("@streamdown/math");
+ mathPluginCache = { math: mod.math };
+ return mathPluginCache;
+ }, []);
+ return plugins;
+};
+
export const MarkdownRenderer = memo(({ content }: { content: string }) => {
+ const plugins = useMathPlugin();
return (
<Streamdown
components={COMPONENTS}
- plugins={{ math }}
+ plugins={plugins}
className="mo-markdown-renderer"
>
{content}