fix(mermaid): isolate diagram layout in a sandboxed iframe

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.
This commit is contained in:
OCILY committed 2026-10-28 20:20:49 +08:00
1 parent 58310536d6
commit fe06211775
12 files changed
+794 -67

No files matched your search

+49
View File
@@ -1921,6 +1921,55 @@ body[data-dsh-sidebar-dragging] .bottomPanel {
height: auto;
}
/* Lazy render placeholder: the fence's diagram is queued and swaps in as soon
as the render queue reaches it (visible diagrams first, the rest while the
browser is idle). The min-height comes from the fence's line count so the
article does not jump under the reader's cursor when the diagram arrives. */
.mermaidPending {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
padding: 18px 24px;
}
.mermaidPendingBar {
display: block;
width: min(320px, 70%);
height: 10px;
border-radius: 5px;
background: var(--dsw-alias-bg-layer-2);
animation: mermaidPendingPulse 1.4s ease-in-out infinite;
}
.mermaidPendingBar:nth-child(2) {
width: min(240px, 55%);
animation-delay: 0.15s;
}
.mermaidPendingBar:nth-child(3) {
width: min(160px, 40%);
animation-delay: 0.3s;
}
@keyframes mermaidPendingPulse {
0%,
100% {
opacity: 0.45;
}
50% {
opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.mermaidPendingBar {
animation: none;
}
}
.mermaidError {
padding: 6px 10px;
border-bottom: 1px solid var(--dsw-alias-border-l1);