Skip to content

Commit c6e404b

Browse files
committed
fix(react-router): stop lazyRouteComponent flashing the error component while reloading
`window.location.reload()` is asynchronous, so renders can still happen between the reload request and the document going away. Those renders re-read the sessionStorage guard, find it already set, and fall through to `throw error`, so the route's error component renders the import failure for a moment before the reload lands. Remember the reload request in the closure and keep suspending on later renders instead of re-evaluating the guard. The sessionStorage guard is untouched, so a chunk missing for any reason other than a new deployment still surfaces its error on the next page load rather than looping. Fixes #8377
1 parent 8e164d2 commit c6e404b

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-router': patch
3+
---
4+
5+
`lazyRouteComponent` no longer flashes the error component while it is reloading after a stale-chunk failure. `window.location.reload()` is asynchronous, so renders can still happen before the document goes away; those renders re-read the `sessionStorage` guard, found it already set, and fell through to `throw error`. The reload request is now remembered in the closure and later renders keep suspending. The `sessionStorage` guard is unchanged, so a chunk that is missing for any reason other than a new deployment still surfaces its error on the next page load instead of reloading in a loop.

‎packages/react-router/src/lazyRouteComponent.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function lazyRouteComponent<
2525
let loadPromise: Promise<any> | undefined
2626
let comp: T[TKey] | T['default']
2727
let error: any
28+
let reloadRequested = false
2829

2930
const load = () => {
3031
if (!loadPromise) {
@@ -51,6 +52,14 @@ export function lazyRouteComponent<
5152
}
5253
const lazyComp = function Lazy(props: any) {
5354
if (error) {
55+
// `location.reload()` is asynchronous, so renders can still happen while
56+
// the document is on its way out. Keep suspending on those instead of
57+
// re-reading the guard below, which is already set and would otherwise
58+
// fall through to `throw error` and flash the error component.
59+
if (reloadRequested) {
60+
throw new Promise(() => {})
61+
}
62+
5463
// A missing module can mean that a newer deployment replaced the URL.
5564
// Reload only for the error that is still current at render time, so a
5665
// successful retry cannot leave a stale reload request armed.
@@ -62,6 +71,7 @@ export function lazyRouteComponent<
6271
const storageKey = `tanstack_router_reload:${error.message}`
6372
if (!sessionStorage.getItem(storageKey)) {
6473
sessionStorage.setItem(storageKey, '1')
74+
reloadRequested = true
6575
window.location.reload()
6676
// Suspend forever while the document reloads.
6777
throw new Promise(() => {})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
2+
import { lazyRouteComponent } from '../src'
3+
4+
let reload: ReturnType<typeof vi.fn>
5+
let originalLocation: Location
6+
7+
beforeEach(() => {
8+
sessionStorage.clear()
9+
reload = vi.fn()
10+
originalLocation = window.location
11+
Object.defineProperty(window, 'location', {
12+
configurable: true,
13+
writable: true,
14+
value: { ...originalLocation, reload },
15+
})
16+
})
17+
18+
afterEach(() => {
19+
Object.defineProperty(window, 'location', {
20+
configurable: true,
21+
writable: true,
22+
value: originalLocation,
23+
})
24+
vi.restoreAllMocks()
25+
})
26+
27+
const chunkError = () =>
28+
new TypeError(
29+
'Failed to fetch dynamically imported module: /assets/posts-BgDSEldj.js',
30+
)
31+
32+
// https://github.com/TanStack/router/issues/8377
33+
test('#8377: renders after the reload is requested keep suspending instead of throwing', async () => {
34+
const Lazy = lazyRouteComponent(() => Promise.reject(chunkError())) as any
35+
36+
await Lazy.preload()
37+
38+
// First render arms the reload and suspends.
39+
let firstThrown: unknown
40+
try {
41+
Lazy({})
42+
} catch (thrown) {
43+
firstThrown = thrown
44+
}
45+
expect(firstThrown).toBeInstanceOf(Promise)
46+
expect(reload).toHaveBeenCalledTimes(1)
47+
48+
// `location.reload()` is async, so React can render again before the
49+
// document goes away. That render must not fall through to `throw error`.
50+
let secondThrown: unknown
51+
try {
52+
Lazy({})
53+
} catch (thrown) {
54+
secondThrown = thrown
55+
}
56+
expect(secondThrown).toBeInstanceOf(Promise)
57+
expect(secondThrown).not.toBeInstanceOf(TypeError)
58+
59+
// Still only the one reload — the sessionStorage guard is untouched.
60+
expect(reload).toHaveBeenCalledTimes(1)
61+
})
62+
63+
// The guard exists to stop a reload loop when the chunk is missing for some
64+
// reason other than a new deployment. A fresh document must still surface the
65+
// error rather than suspending forever.
66+
test('#8377: a later page load still throws once the guard is set', async () => {
67+
const error = chunkError()
68+
sessionStorage.setItem(`tanstack_router_reload:${error.message}`, '1')
69+
70+
const Lazy = lazyRouteComponent(() => Promise.reject(error)) as any
71+
72+
await Lazy.preload()
73+
74+
expect(() => Lazy({})).toThrow(error)
75+
expect(reload).not.toHaveBeenCalled()
76+
})

0 commit comments

Comments
 (0)