Repository navigation
Conversation
Mermaid measures every label with SVGElement.getBBox() against a node appended to document.body, so each measurement flushes the whole host document's layout. Measured in the live DSH window (15,418 DOM nodes): 59-75 ms per measurement, versus 1 ms in an empty iframe document. One diagram therefore took 1.5-7.3 s, and a 31-diagram markdown document kept the renderer main thread blocked for >20 s - long enough for the desktop shell's surface watchdog to kill the renderer, with tab restoration turning it into a crash loop. CSS containment on the app root does not help (59-62 ms); only an independent layout tree does. Render diagrams inside a hidden sandboxed iframe instead: a self-contained blob: document (mermaid UMD bundle + a small postMessage protocol, both inlined by scripts/build-mermaid-frame-asset.mjs) renders each diagram; the parent only sends/receives and sanitizes the SVG. Diagrams now take 30-104 ms each and 31 diagrams complete in ~5 s in the background, while the client mermaid chunk shrinks from 7.04 MB to 3.69 MB (minified UMD replaces the unminified inline bundle). Also add a shared render queue: visible/near-viewport diagrams first, the rest filled in on requestIdleCallback with 16 ms main-thread yields between renders, so every diagram is still rendered (none degrades to a code block). Fence-line-sized placeholder skeletons reserve height while a diagram waits. No host-half route changes are needed.
Follow-up to the iframe isolation: after that fix a 31-diagram document still felt janky while scrolling. The remaining cost was work competing with the scroll gesture: - background diagram renders kept running during scroll (each one injects an SVG, runs DOM passes and lays out); - hidden native tabs kept warming diagrams (tab bodies are keepMounted); - the inline-HTML and heading-anchor MutationObservers ran a full-document scan per mutation (measured 8-15 ms per pass, dozens of passes when a document swaps its diagrams in); - initial near-viewport detection read one rect per diagram between DOM writes, forcing a layout per diagram; - off-screen rendered SVGs still participated in layout and paint. The queue now pauses background work for 350 ms after the last captured scroll event, and only schedules renders whose preview is visible (IntersectionObserver gate per preview; hidden tabs keep placeholders and wake the queue when shown again, and unmounting a preview drops its queued tasks). The two mutation passes coalesce bursts into one 80 ms pass. Initial priorities come from one batched rect read before the swap writes. .mermaidWrap gains content-visibility:auto + contain-intrinsic-size + contain:layout paint style, so off-screen diagrams leave layout and paint. Visible diagrams still render on demand, every diagram is still eventually rendered, and nothing falls back to a code block.
A failed render used to show only a generic "Mermaid render failed" label plus the fence source, with the actual reason hidden in the hover title — which makes a bad fence hard to find. Print the first two lines of mermaid's own message under the label (the full text stays on the title) and cover it in the fallback spec. This was triggered by a real document whose diagram used "A 与 B" instead of mermaid's "A & B" multi-target syntax: the error strip now names the parse error and the source block points at the broken lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fork-internal PR mirroring upstream omdsh-dev#1040 so the same ubuntu + windows CI matrix runs without waiting for upstream workflow approval.