Files
David Luzar b99a49dbeb perf(editor): load Mermaid diagram code per diagram type (mermaid-to-excalidraw 3.1) (#12481)
* perf(editor): bump @excalidraw/mermaid-to-excalidraw to 3.1.0

3.1.0 loads Pele's code per diagram type, on first use (`pele/lazy`),
instead of all 33 diagram types at once: a first flowchart downloads
~66 KiB (brotli) instead of ~217 KiB. The per-diagram chunks are only
reachable through the lib's dynamic import, so `lazyChunksPlugin` keeps
them out of the PWA precache.

Claude Opus 5.5 (xhigh)

* chore(editor): name Pele's diagram chunks in TTD chunk-load comments

Since mermaid-to-excalidraw v3, it's Pele (not Mermaid) that lazy-loads
a chunk per diagram type, as of 3.1.0.

Claude Opus 5.5 (xhigh)

* feat(app): cache all Mermaid chunks for offline use once the TTD dialog opens

mermaid-to-excalidraw 3.1 splits Pele into a chunk per diagram type,
which aren't precached. With 3.0, the single Pele chunk cached on first
use covered every diagram type offline; now only the types converted
while online would.

`lazyChunksPlugin` emits the list of mermaid chunks as a hashed JSON
asset, which index.html points to with a `<meta>` (+33 B br). Once the
TTD dialog is opened, the app fetches the list and the chunks (after 5 s,
so the dialog's own chunks aren't fetched twice, at low priority) for the
service worker to cache in `lazy-chunks` (~200 kB br on top of what the
dialog loads). App only: package hosts (e.g. E+) are unaffected.

Claude Opus 5.5 (xhigh)
2026-10-30 12:10:15 +01:00

43 lines
1.6 KiB
TypeScript

/**
* The name of the `<meta>` that points to the JSON list of the mermaid chunks,
* which `lazyChunksPlugin` (vite.config.mts) emits in production builds.
*/
export const MERMAID_CHUNKS_LIST_NAME = "mermaid-chunks";
// Lets the dialog load the chunks it needs first (mermaid-to-excalidraw, and
// those of the diagram type it converts), so that they aren't fetched twice:
// cached chunks are then served from the cache.
const CACHE_DELAY_MS = 5000;
let caching: Promise<unknown> | null = null;
/**
* The mermaid chunks (mermaid-to-excalidraw, and Pele's chunk per diagram
* type) aren't precached, as most users never use Mermaid. Once the TTD dialog
* is opened, they're all fetched for the service worker to cache (`lazy-chunks`
* runtime caching), so that offline, every diagram type converts, not just the
* ones used while online.
*/
export const cacheMermaidChunks = () => {
const listFileName = document.querySelector<HTMLMetaElement>(
`meta[name="${MERMAID_CHUNKS_LIST_NAME}"]`,
)?.content;
if (caching || !listFileName || !navigator.serviceWorker?.controller) {
return;
}
caching = new Promise((resolve) => setTimeout(resolve, CACHE_DELAY_MS))
.then(() => fetch(`${import.meta.env.BASE_URL}${listFileName}`))
.then((response) => response.json())
.then((fileNames: string[]) =>
Promise.all(
fileNames.map((fileName) =>
fetch(`${import.meta.env.BASE_URL}${fileName}`, { priority: "low" }),
),
),
)
.catch(() => {
// e.g. offline -> retry once the dialog is opened again
caching = null;
});
};