Commit Graph
1 Commits
Author SHA1 Message Date
Alaister YoungandAlaister Young f9e0376951 fix(studio): reload once on chunk load failures (#52380)
When a lazy-loaded JS/CSS chunk fails to load (flaky or mobile
networks), users land on the global error screen and stay there until
they refresh by hand. This reloads the page once instead, in both
runtimes.

A reload is the only reliable recovery: WebKit (Safari and every iOS
browser) caches a failed module fetch in the document's module map, so
every later `import()` of that URL fails instantly until the page is
replaced. TanStack's `lazyRouteComponent` already reloads once for route
chunks, but it keys its guard on the error message, and Safari's message
("Importing a module script failed.") is the same for every chunk and
survives reloads, so after the first time in a tab it never reloads
again. Non-route lazy imports (`next/dynamic`, `React.lazy`, Monaco) had
no recovery at all.

**Added:**
- `lib/chunk-load-error.ts`: `isChunkLoadError` (Chrome, Firefox,
WebKit, webpack `ChunkLoadError` and Vite's CSS preload wording) and a
reload guard
- The guard is time-based (`sessionStorage`, 30s window), not
message-based: a second failure within 30s of a reload shows the normal
error screen instead of looping. It's also capped at 3 automatic reloads
per tab, and the guard is read back after writing it. If storage is
unavailable, or silently drops the write, it never reloads
- Unit tests for every message form, the guard window, the per-tab cap,
and broken, missing or write-dropping storage

**Changed:**
- The global error boundary in `pages/_app.tsx` and `routes/__root.tsx`,
plus TanStack's root `errorComponent`, reload once for chunk-load errors
and render nothing while the reload is in flight. Fallbacks only peek at
the guard during render; the reload is claimed after commit
(`useChunkLoadReload`), so a discarded render can't use up the guard
- Chunk-load errors that a reload recovers from are no longer reported
to Sentry. One that recurs within the guard window (the user actually
saw the error screen) is reported, tagged `chunkLoadError: true`

No `vite:preloadError` listener: it also fires for hover/intent route
preloads, so reloading on it would refresh the page under someone who
only hovered a link.

## To test

- On the preview, open DevTools → Network, block one route chunk URL
(e.g. the SQL editor's), then navigate to it: the page reloads once and
recovers (unblock after the reload), with no error screen flash
- Keep the chunk blocked: after the one reload you get the normal error
screen, not a reload loop
- Repeat after 30s+ in the same tab: it reloads again
- Sentry: the recovered reload sends no event; the persistent failure
sends one tagged `chunkLoadError`
- Normal navigation and genuine non-chunk errors behave as before

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-10-27 20:50:25 +08:00