Review findings on #1040:
- A bad fence no longer poisons the document. `renderMermaidDiagram` used to
treat every `renderInFrame` rejection as a frame failure, so a mermaid
syntax error (a normal `mermaid-error` answer) was retried and then flipped
`frameUnavailable` — every later diagram failed until the page reloaded.
Frame answers are now classified: `MermaidDiagramError` is final for that
one diagram (its block shows the message + source), while
`MermaidFrameError` (load/handshake/timeout/protocol) gets the single retry
and then the page-level fast-fail breaker.
- Both directions of postMessage are validated. Parent side binds
`event.source` to the frame's `contentWindow` plus an opaque-origin
allowlist and validates id/svg/message; the inlined frame script binds
`event.source` to `window.parent` and validates id/code/theme/length before
rendering.
- Visibility gating now flushes on return: the 80ms inline-HTML pass remembers
a skipped pass and replays it on `visibilitychange`, and the preview handle
also pauses/resumes on document visibility (not just IntersectionObserver).
- Tests: new `tests/mermaid-frame.spec.ts` covers the error classes
(diagram error does not break later renders; transport retry; breaker after
two failures) and the message validator (wrong window/origin, malformed
payloads); the asset spec pins the frame-side sender/payload checks.
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.