Skip to content

Commit 8aa846e

Browse files
committed
fix(router-core): detect Webpack/Rspack ChunkLoadError in isModuleNotFoundError
1 parent ac821f4 commit 8aa846e

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

‎packages/router-core/src/utils.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,25 @@ export function createControlledPromise<T>(onResolve?: (value: T) => void) {
487487

488488
/**
489489
* Heuristically detect dynamic import "module not found" errors
490-
* across major browsers for lazy route component handling.
490+
* across major browsers and bundlers for lazy route component handling.
491+
*
492+
* Covers:
493+
* - Chrome: "Failed to fetch dynamically imported module: …"
494+
* - Firefox: "error loading dynamically imported module: …"
495+
* - Safari: "Importing a module script failed."
496+
* - Webpack / Rspack: ChunkLoadError, "Loading chunk … failed.\n(error: …)"
491497
*/
492498
export function isModuleNotFoundError(error: any): boolean {
493-
// chrome: "Failed to fetch dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split"
494-
// firefox: "error loading dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split"
495-
// safari: "Importing a module script failed."
499+
// Webpack / Rspack throw a typed ChunkLoadError with a distinct name
500+
if (error?.name === 'ChunkLoadError') return true
501+
496502
if (typeof error?.message !== 'string') return false
503+
497504
return (
498505
error.message.startsWith('Failed to fetch dynamically imported module') ||
499506
error.message.startsWith('error loading dynamically imported module') ||
500-
error.message.startsWith('Importing a module script failed')
507+
error.message.startsWith('Importing a module script failed') ||
508+
error.message.startsWith('Loading chunk')
501509
)
502510
}
503511

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { isModuleNotFoundError } from '../src/utils'
3+
4+
describe('isModuleNotFoundError', () => {
5+
it('detects Chrome dynamic import error', () => {
6+
const error = new Error(
7+
'Failed to fetch dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split',
8+
)
9+
expect(isModuleNotFoundError(error)).toBe(true)
10+
})
11+
12+
it('detects Firefox dynamic import error', () => {
13+
const error = new Error(
14+
'error loading dynamically imported module: http://localhost:5173/src/routes/posts.index.tsx?tsr-split',
15+
)
16+
expect(isModuleNotFoundError(error)).toBe(true)
17+
})
18+
19+
it('detects Safari dynamic import error', () => {
20+
const error = new Error('Importing a module script failed.')
21+
expect(isModuleNotFoundError(error)).toBe(true)
22+
})
23+
24+
it('detects Webpack ChunkLoadError by name', () => {
25+
// Webpack throws a ChunkLoadError with a distinct name property
26+
const error = new Error('Loading chunk 123 failed.\n(error: http://localhost:3333/js/123.abc123.chunk.js)')
27+
error.name = 'ChunkLoadError'
28+
expect(isModuleNotFoundError(error)).toBe(true)
29+
})
30+
31+
it('detects Webpack-style error by message prefix when name is generic', () => {
32+
// Fallback: even without ChunkLoadError name, the message prefix catches it
33+
const error = new Error('Loading chunk abc123 failed.\n(error: http://localhost:3333/js/main.abc123.js)')
34+
expect(isModuleNotFoundError(error)).toBe(true)
35+
})
36+
37+
it('detects Rspack ChunkLoadError', () => {
38+
const error = new Error('Loading chunk 456 failed.\n(error: http://localhost:3333/js/456.def456.chunk.js)')
39+
error.name = 'ChunkLoadError'
40+
expect(isModuleNotFoundError(error)).toBe(true)
41+
})
42+
43+
it('returns false for null input', () => {
44+
expect(isModuleNotFoundError(null)).toBe(false)
45+
})
46+
47+
it('returns false for undefined input', () => {
48+
expect(isModuleNotFoundError(undefined)).toBe(false)
49+
})
50+
51+
it('returns false for non-error objects', () => {
52+
expect(isModuleNotFoundError({})).toBe(false)
53+
expect(isModuleNotFoundError('string error')).toBe(false)
54+
expect(isModuleNotFoundError(42)).toBe(false)
55+
})
56+
57+
it('returns false for unrelated errors', () => {
58+
const error = new Error('Something went wrong')
59+
expect(isModuleNotFoundError(error)).toBe(false)
60+
})
61+
62+
it('returns false for errors without a message', () => {
63+
const error = {}
64+
expect(isModuleNotFoundError(error)).toBe(false)
65+
})
66+
})

0 commit comments

Comments
 (0)