Build Log

The Pitfalls of Lazy-Loading Mermaid

An empty dashed box labelled "not drawn yet", and across an arrow the same-sized box with the diagram drawn in

I wanted flowcharts and sequence diagrams in a blog post, so I reached for a library that turns text into diagrams right in the browser.

It was convenient, but it turned out to be hiding three separate problems. I fixed them one at a time — and each fix surfaced a new one.

1. Text was invisible in dark mode

Dark mode sequence diagram comparison — on the left, actor names and arrow labels are nearly illegible against the background; on the right, everything reads clearly
Same diagram, same dark palette. The left only passes in the shared color tokens; the right adds 15 sequence-specific variables on top.

Flowcharts rendered fine. Only the sequence diagrams had text that blended into the background.

I was already passing in theme colors:

themeVariables: {
  mainBkg: token('--surface-2'),
  textColor: token('--text'),
  lineColor: token('--text-faint'),
}

Sequence diagrams don't read textColor. They have their own separate set of variables. So the defaults — tuned for a light background — stuck around, and in dark mode that meant dark text on a dark background.

Counting up the variables I actually needed came to fifteen:

actorBkg, actorBorder, actorTextColor, actorLineColor,
signalColor, signalTextColor,
labelBoxBkgColor, labelBoxBorderColor, labelTextColor,
loopTextColor,
noteBkgColor, noteBorderColor, noteTextColor,
activationBkgColor, activationBorderColor,

Actor boxes, arrows, arrow labels, section boxes, loop block text, note boxes, activation bars — each one gets its own color. Miss even one and that single element goes invisible.

2. The raw source flashed before the diagram rendered

The library was heavy, so I lazy-loaded it. That meant the raw, unrendered text briefly showed up as a code block before the diagram appeared.

sequenceDiagram
  participant Phone
  participant Admin
  Phone->>Admin: fix this paragraph

That would flash, then get swapped for the rendered diagram — and the height would jump. A 226px slot became 474px. Whatever line you were reading got shoved down the page.

I tried reserving the height up front with CSS, but it didn't work. Of course it didn't — the element I was trying to size didn't exist until after the library loaded. I was writing CSS for something that wasn't there yet.

So I flipped the order. Create the placeholder shell first, with its size already set, and let the library fill it in afterward. That removed the window where raw source was ever visible at all.

3. Sometimes the diagram just never showed up

The lazy-load strategy was to fetch the library once the diagram got close to the viewport.

new IntersectionObserver(..., { rootMargin: '600px 0px' })

It kicks off 600px before the diagram enters view — one screen-height early. Seemed to work fine.

But during testing I ran into a case where the diagram never rendered at all. Just an empty box, no matter how long I waited.

The cause: the tab was in the background. When document.hidden is true, the browser throttles this observer down to nothing. It can't tell the difference between "off-screen" and "tab hidden."

This is a real scenario, not an edge case — anyone who opens a link in a new tab and comes back to it later has had that tab sitting in the background the whole time. All they see is an empty box.

So I stopped relying on the observer alone.

if (idle) idle(start, { timeout: 4000 })
else setTimeout(start, 2000)

If the browser goes idle, it kicks things off on its own. If 4 seconds pass regardless, it starts anyway. Whichever comes first wins.

What's left

Right now the diagrams still render in the browser. If I rendered them at build time instead and shipped only the output, I wouldn't need to ship the library at all.

That would make both the lazy-loading problem and the background-tab problem disappear together. The one thing I'd still need to figure out is how re-rendering works when the theme changes — so I've shelved it for now.

One takeaway

These three bugs weren't the same kind of bug.

How I found it
Dark mode text Visible at a glance
Height jump Visible at a glance
Empty box Stumbled into it by chance

The first two are obvious the moment you open the page. The third only shows up under specific conditions, and it never throws an error. The screen just shows an empty box, and the user shrugs it off as "guess that's how it's supposed to look."

Bugs like that are the ones that stick around the longest.