Files
sencho/frontend/src/components/isChunkLoadError.ts
T
Anso b843b89ca4 feat(frontend): add LazyBoundary for chunk-load failure recovery (#875)
* feat(frontend): add LazyBoundary for chunk-load failure recovery

When a lazy chunk fetch fails, the existing top-level ErrorBoundary shows
"Something went wrong" with a "Try again" CTA. "Try again" cannot succeed
against a chunk URL that no longer exists on the server (typical post-
deploy case where the user's tab was opened against an older bundle).
The right remedy is to reload the tab so the browser fetches the new
hashed chunks emitted by the current build.

Add LazyBoundary, a section-local error boundary that:

- Detects chunk-load errors via a substring union covering Chrome / Edge
  ("Failed to fetch dynamically imported module"), Safari ("Importing a
  module script failed"), Firefox ("disallowed MIME type" thrown when a
  deploy serves SPA index.html for a missing chunk URL), older Webpack
  ("Error loading dynamically imported module"), and Vite ("Loading
  chunk/CSS chunk N failed").
- For chunk errors, renders a glass-card matching the LockCard aesthetic
  with an AlertTriangle icon, a "This part of Sencho needs a reload"
  message, and a Reload CTA that calls window.location.reload().
- For non-chunk runtime errors, falls back to "Something went wrong" +
  the error message + a Try again CTA. Try again is safe on this path
  because the lazy import has already resolved before the render error
  fires.
- Logs to console.error in componentDidCatch so the underlying failure
  is still observable.
- Has role="alert" so screen readers announce the failure.

Wrap every existing Suspense site (1 in SettingsPage, 7 in EditorLayout
including the security-history overlay, 1 in ResourcesView) with
LazyBoundary. The top-level ErrorBoundary remains the catch-all for
errors that escape the section-local boundary.

Includes a unit test enumerating each browser's documented chunk-load
message so a regression in any one runtime is caught early.

* fix(frontend): move isChunkLoadError to its own file to satisfy react-refresh/only-export-components
2026-05-02 03:40:45 -04:00

31 lines
1.5 KiB
TypeScript

/**
* Recognises the error messages browsers throw when a dynamically-imported
* chunk URL is no longer reachable. Each runtime worded the failure
* differently, so the heuristic is a substring union rather than a single
* regex; broadening to a generic "module + fail" match would over-fire on
* legitimate runtime errors and route them to a misleading Reload CTA.
*
* Currently covered:
* - Chrome / Edge: "Failed to fetch dynamically imported module: <url>"
* - Safari: "Importing a module script failed."
* - Firefox: "Loading module from \"<url>\" was blocked because of a disallowed MIME type (\"text/html\")."
* (fired when a deploy serves the SPA index.html for a missing chunk URL)
* - Older Webpack: "Error loading dynamically imported module"
* - Vite: "Loading chunk N failed." / "Loading CSS chunk N failed"
*
* Add new substrings here as new browser variants surface; cover them in
* the matching unit test so a regression in one runtime is caught early.
*/
export function isChunkLoadError(error: Error | null | undefined): boolean {
if (!error) return false;
const msg = error.message.toLowerCase();
return (
msg.includes('failed to fetch dynamically imported module') ||
msg.includes('importing a module script failed') ||
msg.includes('error loading dynamically imported module') ||
msg.includes('disallowed mime type') ||
msg.includes('loading chunk') ||
msg.includes('loading css chunk')
);
}