Skip to content

Commit f2ab071

Browse files
committed
fix(web): uniquify cached mermaid ids and keep height on theme change
Cached svg markup reused mermaid's t3mermaid ids, so a second visible copy of the same fence could steal markers via url(#…). Clone those ids on each cache read. Keep the last svg while a theme re-render is in flight, and remount on fence content change so the placeholder is only for new diagrams.
1 parent 1c53982 commit f2ab071

4 files changed

Lines changed: 41 additions & 18 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ function MarkdownCodeBlock({
878878
) : null}
879879
{showDiagram ? (
880880
<MermaidDiagram
881+
key={code}
881882
code={code}
882883
language={language}
883884
theme={theme}

‎apps/web/src/components/chat/MermaidDiagram.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function MermaidDiagram({
3333
}
3434

3535
let cancelled = false;
36-
setSvg(null);
3736
setError(null);
3837

3938
void renderMermaidSvg(code, theme).then(

‎apps/web/src/lib/mermaidRenderer.test.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ describe("renderMermaidSvg", () => {
2323
});
2424

2525
it("initializes once per theme and returns the rendered svg", async () => {
26-
mermaid.render.mockResolvedValue({ svg: "<svg>ok</svg>" });
26+
mermaid.render.mockImplementation(async (id: string) => ({
27+
svg: `<svg id="${id}">ok</svg>`,
28+
}));
2729

2830
await expect(renderMermaidSvg("flowchart TD\n A --> B", "dark")).resolves.toBe(
29-
"<svg>ok</svg>",
31+
'<svg id="t3m_1_">ok</svg>',
3032
);
3133
await expect(renderMermaidSvg("flowchart TD\n A --> C", "dark")).resolves.toBe(
32-
"<svg>ok</svg>",
34+
'<svg id="t3m_2_">ok</svg>',
3335
);
3436

3537
expect(mermaid.initialize).toHaveBeenCalledTimes(1);
@@ -45,7 +47,9 @@ describe("renderMermaidSvg", () => {
4547
});
4648

4749
it("reinitializes when the theme changes", async () => {
48-
mermaid.render.mockResolvedValue({ svg: "<svg>ok</svg>" });
50+
mermaid.render.mockImplementation(async (id: string) => ({
51+
svg: `<svg id="${id}">ok</svg>`,
52+
}));
4953

5054
await renderMermaidSvg("flowchart TD\n A --> B", "dark");
5155
await renderMermaidSvg("flowchart TD\n A --> B", "light");
@@ -64,15 +68,19 @@ describe("renderMermaidSvg", () => {
6468
});
6569

6670
it("returns a cached svg without rendering again", async () => {
67-
mermaid.render.mockResolvedValue({ svg: "<svg>ok</svg>" });
71+
mermaid.render.mockImplementation(async (id: string) => ({
72+
svg: `<svg id="${id}"><use href="#${id}-marker"/></svg>`,
73+
}));
6874

69-
await renderMermaidSvg("flowchart TD\n A --> B", "dark");
75+
const first = await renderMermaidSvg("flowchart TD\n A --> B", "dark");
7076
mermaid.render.mockClear();
7177

72-
await expect(renderMermaidSvg("flowchart TD\n A --> B", "dark")).resolves.toBe(
73-
"<svg>ok</svg>",
74-
);
75-
expect(getCachedMermaidSvg("flowchart TD\n A --> B", "dark")).toBe("<svg>ok</svg>");
78+
const second = await renderMermaidSvg("flowchart TD\n A --> B", "dark");
79+
const fromCache = getCachedMermaidSvg("flowchart TD\n A --> B", "dark");
80+
81+
expect(first).toBe('<svg id="t3m_1_"><use href="#t3m_1_-marker"/></svg>');
82+
expect(second).toBe('<svg id="t3m_2_"><use href="#t3m_2_-marker"/></svg>');
83+
expect(fromCache).toBe('<svg id="t3m_3_"><use href="#t3m_3_-marker"/></svg>');
7684
expect(mermaid.render).not.toHaveBeenCalled();
7785
});
7886
});

‎apps/web/src/lib/mermaidRenderer.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { LRUCache } from "./lruCache";
22

33
type MermaidTheme = "light" | "dark";
44

5+
interface CachedMermaidSvg {
6+
svg: string;
7+
rootId: string;
8+
}
9+
510
const MAX_MERMAID_CACHE_ENTRIES = 200;
611
const MAX_MERMAID_CACHE_MEMORY_BYTES = 20 * 1024 * 1024;
712

8-
const mermaidSvgCache = new LRUCache<string>(
13+
const mermaidSvgCache = new LRUCache<CachedMermaidSvg>(
914
MAX_MERMAID_CACHE_ENTRIES,
1015
MAX_MERMAID_CACHE_MEMORY_BYTES,
1116
);
@@ -18,6 +23,15 @@ function mermaidCacheKey(source: string, theme: MermaidTheme): string {
1823
return `${theme}:${source}`;
1924
}
2025

26+
function nextMermaidId(): string {
27+
renderCount += 1;
28+
return `t3m_${renderCount}_`;
29+
}
30+
31+
function cloneMermaidSvg(entry: CachedMermaidSvg): string {
32+
return entry.svg.replaceAll(entry.rootId, nextMermaidId());
33+
}
34+
2135
function loadMermaid(): Promise<typeof import("mermaid")> {
2236
mermaidModulePromise ??= import("mermaid").catch((error: unknown) => {
2337
mermaidModulePromise = null;
@@ -31,12 +45,13 @@ function mermaidApi(mod: typeof import("mermaid")) {
3145
}
3246

3347
export function getCachedMermaidSvg(source: string, theme: MermaidTheme): string | null {
34-
return mermaidSvgCache.get(mermaidCacheKey(source, theme));
48+
const cached = mermaidSvgCache.get(mermaidCacheKey(source, theme));
49+
return cached === null ? null : cloneMermaidSvg(cached);
3550
}
3651

3752
export async function renderMermaidSvg(source: string, theme: MermaidTheme): Promise<string> {
38-
const cached = getCachedMermaidSvg(source, theme);
39-
if (cached !== null) return cached;
53+
const cached = mermaidSvgCache.get(mermaidCacheKey(source, theme));
54+
if (cached !== null) return cloneMermaidSvg(cached);
4055

4156
const mermaid = mermaidApi(await loadMermaid());
4257
if (initializedTheme !== theme) {
@@ -61,9 +76,9 @@ export async function renderMermaidSvg(source: string, theme: MermaidTheme): Pro
6176
initializedTheme = theme;
6277
}
6378

64-
renderCount += 1;
65-
const { svg } = await mermaid.render(`t3mermaid${renderCount}`, source);
66-
mermaidSvgCache.set(mermaidCacheKey(source, theme), svg, svg.length * 2);
79+
const rootId = nextMermaidId();
80+
const { svg } = await mermaid.render(rootId, source);
81+
mermaidSvgCache.set(mermaidCacheKey(source, theme), { svg, rootId }, svg.length * 2);
6782
return svg;
6883
}
6984

0 commit comments

Comments
 (0)