Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
6b4b73d0
Commit
6b4b73d0
authored
Mar 05, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: expand chunk retry coverage and unify retry budget
parent
6adecd80
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
155 additions
and
23 deletions
+155
-23
chunk-load-error-handler.ts
...t/components/chunk-load-error/chunk-load-error-handler.ts
+3
-0
fetch-server-response.ts
...client/components/router-reducer/fetch-server-response.ts
+2
-2
loadable.tsx
packages/next/src/shared/lib/lazy-dynamic/loadable.tsx
+18
-8
loadable.shared-runtime.tsx
packages/next/src/shared/lib/loadable.shared-runtime.tsx
+20
-6
chunk-load-failure.test.ts
.../production/chunk-load-failure/chunk-load-failure.test.ts
+86
-7
pages-async.tsx
.../production/chunk-load-failure/components/pages-async.tsx
+3
-0
pages-dynamic.tsx
test/production/chunk-load-failure/pages/pages-dynamic.tsx
+20
-0
pages-other.tsx
test/production/chunk-load-failure/pages/pages-other.tsx
+3
-0
No files found.
packages/next/src/client/components/chunk-load-error/chunk-load-error-handler.ts
View file @
6b4b73d0
/**
* Shared retry timing utilities for chunk and RSC request retries.
*/
// Retry budget after the initial attempt.
export
const
MAX_RETRY_ATTEMPTS
=
1
const
BASE_DELAY_MS
=
200
const
MAX_JITTER_MS
=
400
const
MAX_RTT_MS
=
500
...
...
packages/next/src/client/components/router-reducer/fetch-server-response.ts
View file @
6b4b73d0
...
...
@@ -31,6 +31,7 @@ import {
}
from
'../app-router-headers'
import
{
isChunkOrNetworkError
}
from
'../chunk-load-error/is-chunk-load-error'
import
{
MAX_RETRY_ATTEMPTS
,
getRetryDelayMs
,
sleep
,
}
from
'../chunk-load-error/chunk-load-error-handler'
...
...
@@ -119,7 +120,6 @@ function doMpaNavigation(url: string): FetchServerResponseResult {
}
let
isPageUnloading
=
false
const
MAX_RSC_FETCH_RETRIES
=
1
if
(
typeof
window
!==
'undefined'
)
{
// Track when the page is unloading, e.g. due to reloading the page or
...
...
@@ -310,7 +310,7 @@ async function fetchServerResponseWithRetry(
err
instanceof
Error
&&
isChunkOrNetworkError
(
err
)
&&
!
isPageUnloading
&&
retryCount
<
MAX_R
SC_FETCH_RETRIE
S
retryCount
<
MAX_R
ETRY_ATTEMPT
S
)
{
// Retry once after a jittered delay for transient failures.
await
sleep
(
getRetryDelayMs
())
...
...
packages/next/src/shared/lib/lazy-dynamic/loadable.tsx
View file @
6b4b73d0
...
...
@@ -4,6 +4,7 @@ import type { ComponentModule } from './types'
import
{
PreloadChunks
}
from
'./preload-chunks'
import
{
isChunkLoadError
}
from
'../../../client/components/chunk-load-error/is-chunk-load-error'
import
{
MAX_RETRY_ATTEMPTS
,
getRetryDelayMs
,
sleep
,
}
from
'../../../client/components/chunk-load-error/chunk-load-error-handler'
...
...
@@ -36,15 +37,24 @@ function convertModule<P>(
*/
function
createRetryableLoader
<
T
>
(
loader
:
()
=>
Promise
<
T
>
):
()
=>
Promise
<
T
>
{
return
async
()
=>
{
try
{
return
await
loader
()
}
catch
(
err
)
{
if
(
typeof
window
!==
'undefined'
&&
isChunkLoadError
(
err
))
{
await
sleep
(
getRetryDelayMs
())
return
loader
()
}
let
retries
=
0
while
(
true
)
{
try
{
return
await
loader
()
}
catch
(
err
)
{
if
(
typeof
window
!==
'undefined'
&&
isChunkLoadError
(
err
)
&&
retries
<
MAX_RETRY_ATTEMPTS
)
{
retries
++
await
sleep
(
getRetryDelayMs
())
continue
}
throw
err
throw
err
}
}
}
}
...
...
packages/next/src/shared/lib/loadable.shared-runtime.tsx
View file @
6b4b73d0
...
...
@@ -26,6 +26,7 @@ import React from 'react'
import
{
LoadableContext
}
from
'./loadable-context.shared-runtime'
import
{
isChunkLoadError
}
from
'../../client/components/chunk-load-error/is-chunk-load-error'
import
{
MAX_RETRY_ATTEMPTS
,
getRetryDelayMs
,
sleep
,
}
from
'../../client/components/chunk-load-error/chunk-load-error-handler'
...
...
@@ -39,14 +40,27 @@ const READY_INITIALIZERS: any[] = []
let
initialized
=
false
function
retryChunkLoadErrorOnce
(
loader
:
any
)
{
return
loader
().
catch
(
async
(
err
:
any
)
=>
{
if
(
typeof
window
!==
'undefined'
&&
isChunkLoadError
(
err
))
{
await
sleep
(
getRetryDelayMs
())
return
loader
()
let
retries
=
0
const
run
=
async
():
Promise
<
any
>
=>
{
try
{
return
await
loader
()
}
catch
(
err
)
{
if
(
typeof
window
!==
'undefined'
&&
isChunkLoadError
(
err
)
&&
retries
<
MAX_RETRY_ATTEMPTS
)
{
retries
++
await
sleep
(
getRetryDelayMs
())
return
run
()
}
throw
err
}
}
throw
err
})
return
run
()
}
function
load
(
loader
:
any
)
{
...
...
test/production/chunk-load-failure/chunk-load-failure.test.ts
View file @
6b4b73d0
...
...
@@ -9,15 +9,13 @@ describe('chunk-load-failure', () => {
files
:
__dirname
,
})
async
function
get
NextDynamicChunk
(
)
{
async
function
get
ChunkContainingText
(
marker
:
string
)
{
const
chunksPath
=
path
.
join
(
next
.
testDir
,
'.next/static/'
)
const
browserChunks
=
await
recursiveReadDir
(
chunksPath
,
{
pathnameFilter
:
(
f
)
=>
/
\.
js$/
.
test
(
f
),
})
let
nextDynamicChunks
=
browserChunks
.
filter
((
f
)
=>
fs
.
readFileSync
(
path
.
join
(
chunksPath
,
f
),
'utf8'
)
.
includes
(
'this is a lazy loaded async component'
)
fs
.
readFileSync
(
path
.
join
(
chunksPath
,
f
),
'utf8'
).
includes
(
marker
)
)
expect
(
nextDynamicChunks
).
toHaveLength
(
1
)
...
...
@@ -25,7 +23,9 @@ describe('chunk-load-failure', () => {
}
it
(
'should report async chunk load failures'
,
async
()
=>
{
let
nextDynamicChunk
=
await
getNextDynamicChunk
()
let
nextDynamicChunk
=
await
getChunkContainingText
(
'this is a lazy loaded async component'
)
let
chunkRequestCount
=
0
let
pageError
:
Error
|
undefined
...
...
@@ -97,7 +97,9 @@ describe('chunk-load-failure', () => {
})
it
(
'should recover after a transient async chunk load failure'
,
async
()
=>
{
let
nextDynamicChunk
=
await
getNextDynamicChunk
()
let
nextDynamicChunk
=
await
getChunkContainingText
(
'this is a lazy loaded async component'
)
let
chunkRequestCount
=
0
let
pageError
:
Error
|
undefined
...
...
@@ -133,8 +135,85 @@ describe('chunk-load-failure', () => {
expect
(
pageError
).
toBeUndefined
()
})
it
(
'should fallback to MPA navigation after a persistent RSC fetch failure'
,
async
()
=>
{
let
rscRequestCount
=
0
const
browser
=
await
next
.
browser
(
'/dynamic'
,
{
beforePageLoad
(
page
)
{
page
.
route
(
'**/other*'
,
async
(
route
)
=>
{
if
(
route
.
request
().
url
().
includes
(
'_rsc='
))
{
rscRequestCount
++
await
route
.
abort
(
'connectionreset'
)
return
}
await
route
.
continue
()
})
},
})
await
browser
.
eval
(
'window.__TEST_NO_RELOAD = true'
)
await
browser
.
elementByCss
(
'#to-other'
).
click
()
await
retry
(
async
()
=>
{
expect
(
await
browser
.
eval
(
'window.location.pathname'
)).
toBe
(
'/other'
)
const
body
=
await
browser
.
elementByCss
(
'body'
)
expect
(
await
body
.
text
()).
toContain
(
'this is other'
)
},
10
_000
,
250
)
// MPA fallback reloads the page context.
expect
(
await
browser
.
eval
(
'window.__TEST_NO_RELOAD'
)).
toBeUndefined
()
// One initial request + one retry attempt.
expect
(
rscRequestCount
).
toBe
(
2
)
})
it
(
'should recover after a transient pages-router chunk load failure'
,
async
()
=>
{
let
pagesDynamicChunk
=
await
getChunkContainingText
(
'this is a pages-router lazy loaded async component'
)
let
chunkRequestCount
=
0
let
pageError
:
Error
|
undefined
const
browser
=
await
next
.
browser
(
'/pages-dynamic'
,
{
beforePageLoad
(
page
)
{
page
.
route
(
`**/
${
pagesDynamicChunk
}
*`
,
async
(
route
)
=>
{
chunkRequestCount
++
if
(
chunkRequestCount
===
1
)
{
await
route
.
abort
(
'connectionreset'
)
return
}
await
route
.
continue
()
})
page
.
on
(
'pageerror'
,
(
error
:
Error
)
=>
{
pageError
=
error
})
},
})
await
retry
(
async
()
=>
{
const
body
=
await
browser
.
elementByCss
(
'body'
)
expect
(
await
body
.
text
()).
toContain
(
'this is a pages-router lazy loaded async component'
)
},
10
_000
,
250
)
// One initial request + one retry attempt.
expect
(
chunkRequestCount
).
toBe
(
2
)
expect
(
pageError
).
toBeDefined
()
expect
(
pageError
.
name
).
toBe
(
'ChunkLoadError'
)
})
it
(
'should report aborted chunks when navigating away'
,
async
()
=>
{
let
nextDynamicChunk
=
await
getNextDynamicChunk
()
let
nextDynamicChunk
=
await
getChunkContainingText
(
'this is a lazy loaded async component'
)
let
resolve
try
{
...
...
test/production/chunk-load-failure/components/pages-async.tsx
0 → 100644
View file @
6b4b73d0
export
default
function
PagesAsync
()
{
return
'this is a pages-router lazy loaded async component'
}
test/production/chunk-load-failure/pages/pages-dynamic.tsx
0 → 100644
View file @
6b4b73d0
import
dynamic
from
'next/dynamic'
import
Link
from
'next/link'
import
{
Suspense
}
from
'react'
const
PagesAsync
=
dynamic
(()
=>
import
(
'../components/pages-async'
),
{
ssr
:
false
,
})
export
default
function
PagesDynamic
()
{
return
(
<>
<
Link
href=
"/pages-other"
id=
"to-pages-other"
prefetch=
{
false
}
>
to pages other
</
Link
>
<
Suspense
fallback=
{
<
div
>
Loading...
</
div
>
}
>
<
PagesAsync
/>
</
Suspense
>
</>
)
}
test/production/chunk-load-failure/pages/pages-other.tsx
0 → 100644
View file @
6b4b73d0
export
default
function
PagesOther
()
{
return
<>
this is pages other
</>
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment