Files
Márk Tolmácsanddwelle 1d4dc2c0e6 feat(editor): Lazy load and don't precache Firebase, lazy load mermaid (#12274)
To reduce the initial bundle size and the PWA precache size, Firebase and Mermaid are now lazy loaded. Both are out of the main bundle and the PWA precache. Each is downloaded the first time it's needed and then cached by the service worker, so it also works offline after that first use.

### How it's done

Rollup splits Mermaid into many chunks with arbitrary names (one per diagram type, plus shared deps), so `globIgnores` can't match them. A small build plugin (`lazyChunksPlugin` in `excalidraw-app/vite.config.mts`) finds the chunks that are only reachable through the dynamic `import("@excalidraw/mermaid-to-excalidraw")`. It then removes them from the precache manifest. Chunks that the main bundle also uses stay precached.

A new `lazy-chunks` runtime cache (`CacheFirst`, `/assets/*.js`, max 200 entries, 90 days) stores any JS chunk that isn't precached the first time it loads.

### Offline behaviour

- **Mermaid / text-to-diagram** is no longer cached at install. It works offline once it has been opened online at least once.
- While the Mermaid library loads, the preview shows a spinner (it used to flash an error).
- If the library fails to load:
  - **Offline:** the preview shows "Couldn't load Mermaid. Check your internet connection and try again." with a Retry button. It also retries automatically when the browser comes back online.
  - **Online** (e.g. a stale app version after a deploy): the preview shows "Couldn't load Mermaid. Reload the page to try again." On `vite:preloadError` the service worker checks for an update.
  - **Chrome** keeps a failed dynamic import cached for the life of the page, so retrying can't work there. In Chrome the button reloads the page instead. The Mermaid definition being edited is saved before unload, so it isn't lost.
  - Inserting a generated diagram while the library is unavailable shows a toast with the same message. If AI generation finishes while the library can't load, the Mermaid output is validated once the library loads.
  - Pasting a Mermaid definition while the library can't load pastes it as plain text instead of failing.
- **Share / live collaboration** is hidden while offline, unless you're already in a session. This covers the top-right Share button, the main menu, the welcome screen and the command palette. An open Share dialog closes when you go offline. The online/offline state is read at startup, and changes to it are debounced by 1 s.
- **Not precached:** Mermaid and `firebaseSDK-*.js` are newly excluded. CodeMirror, font subsetting, QR code and locales were already excluded on `master`.

---------

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
2026-10-21 12:36:40 +02:00
..