Skip to content
Source
Sign inSign up
TanStack/router

fix(router-core): detect Webpack/Rspack ChunkLoadError in isModuleNotFoundError #7653

Closedsijie-Z(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/7653/headopened

Showing 2 changed files with 79 additions and 5 deletions.

x
0 of 2 viewed, 2 left
13 additions and 5 deletionspackages/router-core/src/utils.tsModified
View file
@@ -487,17 +487,25 @@
​
/**
* Heuristically detect dynamic import "module not found" errors
- * across major browsers for lazy route component handling.
+ * across major browsers and bundlers for lazy route component handling.
+ *
+ * Covers:
+ * - Chrome: "Failed to fetch dynamically imported module: …"
+ * - Firefox: "error loading dynamically imported module: …"
+ * - Safari: "Importing a module script failed."
+ * - Webpack / Rspack: ChunkLoadError, "Loading chunk … failed.\n(error: …)"
export function isModuleNotFoundError(error: any): boolean {
- // chrome: "Failed to fetch dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split"
- // firefox: "error loading dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split"
- // safari: "Importing a module script failed."
+ // Webpack / Rspack throw a typed ChunkLoadError with a distinct name
+ if (error?.name === 'ChunkLoadError') return true
+​
if (typeof error?.message !== 'string') return false
+​
return (
error.message.startsWith('Failed to fetch dynamically imported module') ||
error.message.startsWith('error loading dynamically imported module') ||
- error.message.startsWith('Importing a module script failed')
+ error.message.startsWith('Importing a module script failed') ||
+ error.message.startsWith('Loading chunk')