Skip to content

fix: mermaid racing conditions when multiple diagrams are present#1385

Open
HugoHSun wants to merge 2 commits intonextfrom
hugo/cx-2822-mermaid-diagrams-intermittently-overlap-break-layout-when-4
Open

fix: mermaid racing conditions when multiple diagrams are present#1385
HugoHSun wants to merge 2 commits intonextfrom
hugo/cx-2822-mermaid-diagrams-intermittently-overlap-break-layout-when-4

Conversation

@HugoHSun
Copy link
Contributor

@HugoHSun HugoHSun commented Mar 19, 2026

PR App Fix CX-2822

🧰 Changes

Pages with multiple mermaid diagrams would intermittently render broken on refresh. Two or more diagrams would overlap into the same <pre> element while another would be empty.

The root cause is mermaid's ID generation. It uses Date.now() to generate SVG IDs, and when multiple diagrams are processed in the same millisecond they get identical IDs. Since mermaid uses document.getElementById() to clean up before rendering, a duplicate ID causes it to delete the wrong SVG from the DOM.

Previously, each CodeTabs instance called mermaid.run() independently with all mermaid nodes on the page. Now mermaid nodes are batched into a single mermaid.run() call using a setTimeout(0) to collect all nodes from the same render cycle. Combined with deterministicIds: true, this gives every diagram in the batch a unique ID.

🧬 QA & Testing

https://www.loom.com/share/376c0ae07c0142d083a6cadf99889e9f

Add multiple mermaid charts in a doc and refresh multiple times

@HugoHSun HugoHSun marked this pull request as ready for review March 20, 2026 00:23
@HugoHSun HugoHSun enabled auto-merge (squash) March 20, 2026 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant