Repository navigation
Mermaid diagram disappears after blurring from edit mode - #953
Conversation
Root cause: mermaid.render() uses the element ID to create a temporary DOM
element during rendering. When blur triggers rapid re-renders (the $effect
fires multiple times as isEditing changes propagate across all mermaid nodes),
two concurrent render calls for the same node share the same element ID
(mermaid-{nodeId}). The first render's result gets discarded as stale by the
sequence guard, and the second render fails because the ID already exists in
the DOM — causing mermaid to throw and return null, clearing the diagram.
Fix: use a unique render ID per call (mermaid-{nodeId}-{timestamp}) so
concurrent renders never collide. The temp element is cleaned up after each
successful render.
Co-Authored-By: Claude <[email protected]>
Code Review SummaryThis PR addresses a real, reproducible bug: mermaid diagrams disappearing after blur from edit mode. The change is a single-file, 7-line diff to Background context: The underlying stale-result guard fix was already landed on Verdict: This is a net improvement. The root cause identified in the commit message is accurate and the fix is correct. Approved with one improvement and one nit noted below. FindingsSuggested Improvements
The The cleanup should be placed in a const renderId = `mermaid-${id}-${Date.now()}`;
try {
const { svg } = await mermaid.render(renderId, definition);
return sanitizeSvg(svg);
} finally {
document.getElementById(renderId)?.remove();
}This is a correctness issue in the error path. With the current structure you'd need to restructure the existing try/catch slightly, but the intent is clear. Recommend fixing before merge. Nitpicks
Acceptance Criteria Check
The improvement around the |
Use try/finally for temp element cleanup so it always runs even when mermaid.render() throws. Also replace Date.now() with a module-level monotonic counter to guarantee unique render IDs with no ms-precision collision risk. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
Addressed both review comments in commit 27111ef: 🟡 DOM temp element leak on render failure — Wrapped 🟢 Monotonic counter instead of Date.now() — Added a module-level |
* Fix mermaid diagram disappearing after blur from edit mode (closes #938) Root cause: mermaid.render() uses the element ID to create a temporary DOM element during rendering. When blur triggers rapid re-renders (the $effect fires multiple times as isEditing changes propagate across all mermaid nodes), two concurrent render calls for the same node share the same element ID (mermaid-{nodeId}). The first render's result gets discarded as stale by the sequence guard, and the second render fails because the ID already exists in the DOM — causing mermaid to throw and return null, clearing the diagram. Fix: use a unique render ID per call (mermaid-{nodeId}-{timestamp}) so concurrent renders never collide. The temp element is cleaned up after each successful render. Co-Authored-By: Claude <[email protected]> * Fix DOM temp element leak on Mermaid render failure Use try/finally for temp element cleanup so it always runs even when mermaid.render() throws. Also replace Date.now() with a module-level monotonic counter to guarantee unique render IDs with no ms-precision collision risk. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Closes #938