The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 12:00 UTC
perf(web): revisiting a thread reuses its rendered Markdown instead of parsing again (#27480)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 30, 2026 at 09:39 UTC
78fba059a1c40a114058bfdddd2f99488cc641a4
5 files changed
+253
-41
apps/web/package.json
+4
@@ -61,6 +61,7 @@
61
"hast-util-to-html": "^9.0.5",
62
"hast-util-to-jsx-runtime": "^2.3.6",
63
"heic-to": "^1.5.2",
64
+ "html-url-attributes": "^3.0.1",
65
"jose": "catalog:",
66
"jsonc-parser": "3.3.1",
67
"jszip": "3.10.1",
@@ -76,6 +77,7 @@
77
"remark-gfm": "catalog:",
78
"remark-math": "6.0.0",
79
"remark-parse": "catalog:",
80
+ "remark-rehype": "catalog:",
81
"saxes": "6.0.0",
82
"sql.js": "1.15.0",
83
"tailwind-merge": "^3.4.0",
@@ -83,6 +85,7 @@
85
"unbash": "^5.0.0",
86
"unified": "catalog:",
87
"use-sync-external-store": "^1.6.0",
88
+ "vfile": "^6.0.3",
89
"zustand": "^5.0.11"
90
},
91
"devDependencies": {
@@ -92,6 +95,7 @@
95
"@tanstack/router-plugin": "^1.161.0",
96
"@types/compression": "^1.8.1",
97
"@types/culori": "^4.0.1",
98
+ "@types/hast": "^3.0.4",
99
"@types/mdast": "^4.0.4",
100
"@types/react": "catalog:",
101
"@types/react-dom": "catalog:",
apps/web/src/components/ChatMarkdown.tsx
+83
-41
@@ -87,8 +87,8 @@ import type {
87
ExtraProps as ReactMarkdownExtraProps,
88
Options as ReactMarkdownOptions,
89
} from "react-markdown";
90
-import ReactMarkdown from "react-markdown";
90
import { createIncrementalMarkdownPlugin } from "../markdown-incremental";
91
+import type { Pluggable, PluggableList } from "unified";
92
import { defaultUrlTransform } from "react-markdown";
93
import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations";
94
import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
@@ -189,6 +189,7 @@ import {
189
import { resolveLinkTarget } from "../browser/browserLinkTarget";
190
import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
191
import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock";
192
+import { createMarkdownRenderer } from "./chat/markdownRenderer";
193
import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks";
194
import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
195
import { MarkdownTable } from "./chat/MarkdownTable";
@@ -397,6 +398,14 @@ const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [
398
rehypeTextDirection,
399
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
400
401
+function markdownUrlTransform(href: string) {
402
+ if (parseAssistantCitationHref(href)) return href;
403
+ if (parseComposerContextHref(href)) return href;
404
+ if (parseThreadLinkHref(href)) return href;
405
+ if (isWindowsDrivePathHref(href)) return href;
406
+ return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
407
+}
408
+
409
const NO_MARKDOWN_EDITORS: ReadonlyArray<EditorId> = [];
410
411
type MarkdownServerConfigFields = ReturnType<typeof markdownServerConfigFields>;
@@ -644,13 +653,6 @@ function useChatMarkdownState({
653
});
654
};
655
const diffThemeName = resolveDiffThemeName(resolvedTheme);
647
- const markdownUrlTransform = (href: string) => {
648
- if (parseAssistantCitationHref(href)) return href;
649
- if (parseComposerContextHref(href)) return href;
650
- if (parseThreadLinkHref(href)) return href;
651
- if (isWindowsDrivePathHref(href)) return href;
652
- return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
653
- };
656
const handleCopy = (event: ReactClipboardEvent<HTMLElement>) => {
657
const selection = window.getSelection();
658
if (!selection || selection.isCollapsed || !event.clipboardData) return;
@@ -925,7 +927,6 @@ function useChatMarkdownState({
927
componentState,
928
handleCopy,
929
markdownRef,
928
- markdownUrlTransform,
930
localMediaPreview,
931
closeLocalMediaPreview,
932
};
@@ -1633,29 +1634,62 @@ const CHAT_MARKDOWN_COMPONENTS = {
1634
},
1635
} satisfies Components;
1636
1637
+const REMARK_MATH: Pluggable = [remarkMath, { singleDollarTextMath: true }];
1638
+// Shared lists, so every message with the same options shares a processor and its cache.
1639
+const CHAT_MARKDOWN_REMARK_PLUGIN_SETS = {
1640
+ plain: CHAT_MARKDOWN_REMARK_PLUGINS,
1641
+ breaks: CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
1642
+ math: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS],
1643
+ mathBreaks: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS],
1644
+} satisfies Record<string, PluggableList>;
1645
+
1646
+const markdownRenderers = new WeakMap<
1647
+ PluggableList,
1648
+ WeakMap<PluggableList, ReturnType<typeof createMarkdownRenderer>>
1649
+>();
1650
+
1651
+function markdownRenderer(remarkPlugins: PluggableList, rehypePlugins: PluggableList) {
1652
+ let byRehype = markdownRenderers.get(remarkPlugins);
1653
+ if (!byRehype) markdownRenderers.set(remarkPlugins, (byRehype = new WeakMap()));
1654
+ let renderer = byRehype.get(rehypePlugins);
1655
+ if (!renderer) {
1656
+ renderer = createMarkdownRenderer({
1657
+ remarkPlugins,
1658
+ rehypePlugins,
1659
+ components: CHAT_MARKDOWN_COMPONENTS,
1660
+ urlTransform: markdownUrlTransform,
1661
+ });
1662
+ byRehype.set(rehypePlugins, renderer);
1663
+ }
1664
+ return renderer;
1665
+}
1666
+
1667
const MarkdownSource = memo(function MarkdownSource({
1668
text,
1669
remarkPlugins,
1670
rehypePlugins,
1640
- urlTransform,
1671
+ shared,
1672
+ cache,
1673
}: {
1674
text: string;
1643
- remarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
1644
- rehypePlugins: NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
1645
- urlTransform: ReactMarkdownOptions["urlTransform"];
1675
+ remarkPlugins: PluggableList;
1676
+ rehypePlugins: PluggableList;
1677
+ /** The plugins are one of the shared sets. */
1678
+ shared: boolean;
1679
+ /** The text will not change again; streamed text would only fill the cache with prefixes. */
1680
+ cache: boolean;
1681
}) {
1682
+ // Per-instance plugins, such as the incremental parser, set up their state on every render.
1683
+ const render = shared
1684
+ ? markdownRenderer(remarkPlugins, rehypePlugins)
1685
+ : createMarkdownRenderer({
1686
+ remarkPlugins,
1687
+ rehypePlugins,
1688
+ components: CHAT_MARKDOWN_COMPONENTS,
1689
+ urlTransform: markdownUrlTransform,
1690
+ });
1691
return (
1648
- <ChatMarkdownTextContext value={text}>
1649
- <ReactMarkdown
1650
- remarkPlugins={remarkPlugins}
1651
- rehypePlugins={rehypePlugins}
1652
- skipHtml={false}
1653
- components={CHAT_MARKDOWN_COMPONENTS}
1654
- urlTransform={urlTransform}
1655
- >
1656
- {text}
1657
- </ReactMarkdown>
1658
- </ChatMarkdownTextContext>
1692
+ <ChatMarkdownTextContext value={text}>{render(text, shared && cache)}</ChatMarkdownTextContext>
1693
);
1694
});
1695
@@ -1668,14 +1702,8 @@ function ChatMarkdown({
1702
extraRemarkPlugins = EMPTY_REMARK_PLUGINS,
1703
...props
1704
}: ChatMarkdownProps) {
1671
- const {
1672
- componentState,
1673
- handleCopy,
1674
- markdownRef,
1675
- markdownUrlTransform,
1676
- localMediaPreview,
1677
- closeLocalMediaPreview,
1678
- } = useChatMarkdownState({ text, inline, ...props });
1705
+ const { componentState, handleCopy, markdownRef, localMediaPreview, closeLocalMediaPreview } =
1706
+ useChatMarkdownState({ text, inline, ...props });
1707
// Streaming re-renders only the last block; offsets for task edits need the whole text.
1708
const streamingBlocks =
1709
props.isStreaming === true &&
@@ -1693,13 +1721,26 @@ function ChatMarkdown({
1721
const bindMarkdownRef = (node: HTMLElement | null) => {
1722
markdownRef.current = node;
1723
};
1696
- const remarkPlugins = [
1697
- ...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS),
1698
- ...extraRemarkPlugins,
1699
- ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
1700
- ];
1701
- if (componentState.mathEnabled)
1702
- remarkPlugins.unshift([remarkMath, { singleDollarTextMath: true }]);
1724
+ const sharedRemarkPlugins =
1725
+ CHAT_MARKDOWN_REMARK_PLUGIN_SETS[
1726
+ componentState.mathEnabled
1727
+ ? lineBreaks
1728
+ ? "mathBreaks"
1729
+ : "math"
1730
+ : lineBreaks
1731
+ ? "breaks"
1732
+ : "plain"
1733
+ ];
1734
+ // Extra and incremental plugins are per instance, so their output is not shared.
1735
+ const uncachedPlugins = extraRemarkPlugins.length > 0 || incrementalParsing;
1736
+ const remarkPlugins = uncachedPlugins
1737
+ ? [
1738
+ ...sharedRemarkPlugins,
1739
+ ...extraRemarkPlugins,
1740
+ ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
1741
+ ]
1742
+ : sharedRemarkPlugins;
1743
+ const blocks = streamingBlocks ?? [text];
1744
1745
return (
1746
<Root
@@ -1714,7 +1755,7 @@ function ChatMarkdown({
1755
onCopy={handleCopy}
1756
>
1757
<ChatMarkdownRendererContext value={componentState}>
1717
- {(streamingBlocks ?? [text]).map((source, index) => (
1758
+ {blocks.map((source, index) => (
1759
<MarkdownSource
1760
// oxlint-disable-next-line react/no-array-index-key -- streamed blocks only change at the end
1761
key={index}
@@ -1725,7 +1766,8 @@ function ChatMarkdown({
1766
? CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS
1767
: CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
1768
}
1728
- urlTransform={markdownUrlTransform}
1769
+ shared={!uncachedPlugins}
1770
+ cache={!props.isStreaming}
1771
/>
1772
))}
1773
</ChatMarkdownRendererContext>
apps/web/src/components/chat/markdownRenderer.test.tsx
new
+49
@@ -0,0 +1,49 @@
1
+import {
2
+ CHAT_MARKDOWN_REHYPE_PLUGINS,
3
+ CHAT_MARKDOWN_REMARK_PLUGINS,
4
+} from "@t3tools/shared/markdownPipeline";
5
+import { renderToStaticMarkup } from "react-dom/server";
6
+import ReactMarkdown from "react-markdown";
7
+import { describe, expect, it } from "vite-plus/test";
8
+
9
+import { createMarkdownRenderer } from "./markdownRenderer";
10
+
11
+// Visible in the markup, so every URL attribute must pass through it.
12
+const urlTransform = (url: string) => `${url}#seen`;
13
+
14
+describe("createMarkdownRenderer", () => {
15
+ const render = createMarkdownRenderer({
16
+ remarkPlugins: CHAT_MARKDOWN_REMARK_PLUGINS,
17
+ rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
18
+ components: {},
19
+ urlTransform,
20
+ });
21
+
22
+ it.each([
23
+ '# Title\n\nText with `code`, **bold**, [a link](https://example.com "t") and https://bare.example.com.',
24
+ " and [file](file:///C:/repo/a.ts) and [bad](javascript:alert(1))",
25
+ "<details><summary>More</summary>\n\nHidden <b>bold</b> <script>x()</script>\n\n</details>",
26
+ "| a | b |\n| - | - |\n| <br> | [x](#frag) |\n\n- [ ] task\n- [x] done",
27
+ '```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful <a>bare</a>',
28
+ "Footnote[^1] and ~~strike~~\n\n[^1]: the note",
29
+ ])("renders what react-markdown renders: %j", (source) => {
30
+ const expected = renderToStaticMarkup(
31
+ <ReactMarkdown
32
+ remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
33
+ rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
34
+ urlTransform={urlTransform}
35
+ >
36
+ {source}
37
+ </ReactMarkdown>,
38
+ );
39
+ const element = render(source, true);
40
+ expect(renderToStaticMarkup(element)).toBe(expected);
41
+ expect(render(source, true)).toBe(element);
42
+ });
43
+
44
+ it("keeps cached messages when one far larger than the cache is rendered", () => {
45
+ const element = render("Kept *message*", true);
46
+ render("x".repeat(600_000), true);
47
+ expect(render("Kept *message*", true)).toBe(element);
48
+ });
49
+});
apps/web/src/components/chat/markdownRenderer.ts
new
+105
@@ -0,0 +1,105 @@
1
+import type { Element, Nodes, Root } from "hast";
2
+import { toJsxRuntime, type Components } from "hast-util-to-jsx-runtime";
3
+import { urlAttributes } from "html-url-attributes";
4
+import type { ReactElement } from "react";
5
+import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
+import remarkParse from "remark-parse";
7
+import remarkRehype from "remark-rehype";
8
+import { type PluggableList, unified } from "unified";
9
+import { VFile } from "vfile";
10
+
11
+export type MarkdownUrlTransform = (url: string, key: string, node: Element) => string;
12
+
13
+interface MarkdownRendererOptions {
14
+ readonly remarkPlugins: PluggableList;
15
+ readonly rehypePlugins: PluggableList;
16
+ readonly components: Partial<Components>;
17
+ readonly urlTransform: MarkdownUrlTransform;
18
+}
19
+
20
+// Elements are immutable, so a remount (a thread switch, a row scrolled back into view)
21
+// reuses one instead of parsing again. Parsing is most of the cost of rendering markdown.
22
+// An element and the hast it keeps hold about 20 bytes per source character, so ~10 MB.
23
+const MAX_CACHED_CHARS = 500_000;
24
+const rendered = new Map<string, ReactElement>();
25
+let cachedChars = 0;
26
+let nextRendererId = 0;
27
+
28
+// A short block still holds a whole element tree.
29
+const ENTRY_OVERHEAD_CHARS = 200;
30
+const entrySize = (key: string) => key.length + ENTRY_OVERHEAD_CHARS;
31
+
32
+function remember(key: string, element: ReactElement) {
33
+ // One huge message would otherwise evict everything else.
34
+ if (entrySize(key) > MAX_CACHED_CHARS / 4) return;
35
+ rendered.set(key, element);
36
+ cachedChars += entrySize(key);
37
+ for (const oldest of rendered.keys()) {
38
+ if (cachedChars <= MAX_CACHED_CHARS) break;
39
+ rendered.delete(oldest);
40
+ cachedChars -= entrySize(oldest);
41
+ }
42
+}
43
+
44
+/** Renders markdown as react-markdown does, with one processor per plugin set. */
45
+export function createMarkdownRenderer({
46
+ remarkPlugins,
47
+ rehypePlugins,
48
+ components,
49
+ urlTransform,
50
+}: MarkdownRendererOptions) {
51
+ const processor = unified()
52
+ .use(remarkParse)
53
+ .use(remarkPlugins)
54
+ .use(remarkRehype, { allowDangerousHtml: true })
55
+ .use(rehypePlugins)
56
+ .freeze();
57
+ const keyPrefix = `${nextRendererId++}\u0000`;
58
+
59
+ // Literal raw HTML that survived the rehype plugins, and URL attributes, as react-markdown treats them.
60
+ const prepare = (node: Nodes) => {
61
+ if (!("children" in node)) return;
62
+ node.children.forEach((child, index) => {
63
+ if (child.type === "raw") {
64
+ node.children[index] = { type: "text", value: child.value };
65
+ return;
66
+ }
67
+ if (child.type === "element") {
68
+ for (const [key, tags] of Object.entries(urlAttributes)) {
69
+ if (
70
+ Object.hasOwn(child.properties, key) &&
71
+ (tags === null || tags.includes(child.tagName))
72
+ ) {
73
+ child.properties[key] = urlTransform(String(child.properties[key] || ""), key, child);
74
+ }
75
+ }
76
+ }
77
+ prepare(child);
78
+ });
79
+ };
80
+
81
+ return (text: string, cache: boolean): ReactElement => {
82
+ const key = keyPrefix + text;
83
+ const cached = rendered.get(key);
84
+ if (cached !== undefined) {
85
+ // Refresh its place in the eviction order.
86
+ rendered.delete(key);
87
+ rendered.set(key, cached);
88
+ return cached;
89
+ }
90
+ const file = new VFile(text);
91
+ const tree = processor.runSync(processor.parse(file), file) as Root;
92
+ prepare(tree);
93
+ const element = toJsxRuntime(tree, {
94
+ Fragment,
95
+ components,
96
+ ignoreInvalidStyle: true,
97
+ jsx,
98
+ jsxs,
99
+ passKeys: true,
100
+ passNode: true,
101
+ });
102
+ if (cache) remember(key, element);
103
+ return element;
104
+ };
105
+}
pnpm-lock.yaml
+12
@@ -1150,6 +1150,9 @@ importers:
1150
heic-to:
1151
specifier: ^1.5.2
1152
version: 1.5.2
1153
+ html-url-attributes:
1154
+ specifier: ^3.0.1
1155
+ version: 3.0.1
1156
jose:
1157
specifier: 'catalog:'
1158
version: 6.2.2
@@ -1195,6 +1198,9 @@ importers:
1198
remark-parse:
1199
specifier: 'catalog:'
1200
version: 11.0.0(supports-color@8.1.1)
1201
+ remark-rehype:
1202
+ specifier: 'catalog:'
1203
+ version: 11.1.2
1204
saxes:
1205
specifier: 6.0.0
1206
version: 6.0.0
@@ -1216,6 +1222,9 @@ importers:
1222
use-sync-external-store:
1223
specifier: ^1.6.0
1224
version: 1.6.0(react@19.3.0)
1225
+ vfile:
1226
+ specifier: ^6.0.3
1227
+ version: 6.0.3
1228
zustand:
1229
specifier: ^5.0.11
1230
version: 5.0.14(@types/react@19.3.0)(react@19.3.0)(use-sync-external-store@1.6.0(react@19.3.0))
@@ -1238,6 +1247,9 @@ importers:
1247
'@types/culori':
1248
specifier: ^4.0.1
1249
version: 4.0.1
1250
+ '@types/hast':
1251
+ specifier: ^3.0.4
1252
+ version: 3.0.5
1253
'@types/mdast':
1254
specifier: ^4.0.4
1255
version: 4.0.4