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
bda731fa
Unverified
Commit
bda731fa
authored
May 19, 2025
by
Hendrik Liebau
Committed by
GitHub
May 19, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client router should discard stale prefetch entries for static pages (#79362)
Backport of #79309 to 15.3.x
parent
d9ec4a4b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
56 deletions
+78
-56
fetch-server-response.ts
...client/components/router-reducer/fetch-server-response.ts
+6
-2
index.ts
packages/next/src/export/index.ts
+1
-0
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+16
-9
types.ts
packages/next/src/server/app-render/types.ts
+5
-1
base-server.ts
packages/next/src/server/base-server.ts
+1
-0
prefetching.stale-times.test.ts
.../e2e/app-dir/app-prefetch/prefetching.stale-times.test.ts
+49
-44
No files found.
packages/next/src/client/components/router-reducer/fetch-server-response.ts
View file @
bda731fa
...
@@ -187,9 +187,13 @@ export async function fetchServerResponse(
...
@@ -187,9 +187,13 @@ export async function fetchServerResponse(
const
contentType
=
res
.
headers
.
get
(
'content-type'
)
||
''
const
contentType
=
res
.
headers
.
get
(
'content-type'
)
||
''
const
interception
=
!!
res
.
headers
.
get
(
'vary'
)?.
includes
(
NEXT_URL
)
const
interception
=
!!
res
.
headers
.
get
(
'vary'
)?.
includes
(
NEXT_URL
)
const
postponed
=
!!
res
.
headers
.
get
(
NEXT_DID_POSTPONE_HEADER
)
const
postponed
=
!!
res
.
headers
.
get
(
NEXT_DID_POSTPONE_HEADER
)
const
staleTimeHeader
=
res
.
headers
.
get
(
NEXT_ROUTER_STALE_TIME_HEADER
)
const
staleTimeHeaderSeconds
=
res
.
headers
.
get
(
NEXT_ROUTER_STALE_TIME_HEADER
)
const
staleTime
=
const
staleTime
=
staleTimeHeader
!==
null
?
parseInt
(
staleTimeHeader
,
10
)
:
-
1
staleTimeHeaderSeconds
!==
null
?
parseInt
(
staleTimeHeaderSeconds
,
10
)
*
1000
:
-
1
let
isFlightResponse
=
contentType
.
startsWith
(
RSC_CONTENT_TYPE_HEADER
)
let
isFlightResponse
=
contentType
.
startsWith
(
RSC_CONTENT_TYPE_HEADER
)
if
(
process
.
env
.
NODE_ENV
===
'production'
)
{
if
(
process
.
env
.
NODE_ENV
===
'production'
)
{
...
...
packages/next/src/export/index.ts
View file @
bda731fa
...
@@ -390,6 +390,7 @@ async function exportAppImpl(
...
@@ -390,6 +390,7 @@ async function exportAppImpl(
experimental
:
{
experimental
:
{
clientTraceMetadata
:
nextConfig
.
experimental
.
clientTraceMetadata
,
clientTraceMetadata
:
nextConfig
.
experimental
.
clientTraceMetadata
,
expireTime
:
nextConfig
.
expireTime
,
expireTime
:
nextConfig
.
expireTime
,
staleTimes
:
nextConfig
.
experimental
.
staleTimes
,
dynamicIO
:
nextConfig
.
experimental
.
dynamicIO
??
false
,
dynamicIO
:
nextConfig
.
experimental
.
dynamicIO
??
false
,
clientSegmentCache
:
clientSegmentCache
:
nextConfig
.
experimental
.
clientSegmentCache
===
'client-only'
nextConfig
.
experimental
.
clientSegmentCache
===
'client-only'
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
bda731fa
...
@@ -2745,6 +2745,12 @@ async function prerenderToStream(
...
@@ -2745,6 +2745,12 @@ async function prerenderToStream(
setMetadataHeader
(
name
)
setMetadataHeader
(
name
)
}
}
const
selectStaleTime
=
(
stale
:
number
)
=>
stale
===
INFINITE_CACHE
&&
typeof
renderOpts
.
experimental
.
staleTimes
?.
static
===
'number'
?
renderOpts
.
experimental
.
staleTimes
.
static
:
stale
let
prerenderStore
:
PrerenderStore
|
null
=
null
let
prerenderStore
:
PrerenderStore
|
null
=
null
try
{
try
{
...
@@ -3166,7 +3172,7 @@ async function prerenderToStream(
...
@@ -3166,7 +3172,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
finalRenderPrerenderStore
.
revalidate
,
collectedRevalidate
:
finalRenderPrerenderStore
.
revalidate
,
collectedExpire
:
finalRenderPrerenderStore
.
expire
,
collectedExpire
:
finalRenderPrerenderStore
.
expire
,
collectedStale
:
finalRenderPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
finalRenderPrerenderStore
.
stale
)
,
collectedTags
:
finalRenderPrerenderStore
.
tags
,
collectedTags
:
finalRenderPrerenderStore
.
tags
,
}
}
}
else
{
}
else
{
...
@@ -3228,7 +3234,7 @@ async function prerenderToStream(
...
@@ -3228,7 +3234,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
finalRenderPrerenderStore
.
revalidate
,
collectedRevalidate
:
finalRenderPrerenderStore
.
revalidate
,
collectedExpire
:
finalRenderPrerenderStore
.
expire
,
collectedExpire
:
finalRenderPrerenderStore
.
expire
,
collectedStale
:
finalRenderPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
finalRenderPrerenderStore
.
stale
)
,
collectedTags
:
finalRenderPrerenderStore
.
tags
,
collectedTags
:
finalRenderPrerenderStore
.
tags
,
}
}
}
}
...
@@ -3653,7 +3659,7 @@ async function prerenderToStream(
...
@@ -3653,7 +3659,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
finalServerPrerenderStore
.
revalidate
,
collectedRevalidate
:
finalServerPrerenderStore
.
revalidate
,
collectedExpire
:
finalServerPrerenderStore
.
expire
,
collectedExpire
:
finalServerPrerenderStore
.
expire
,
collectedStale
:
finalServerPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
finalServerPrerenderStore
.
stale
)
,
collectedTags
:
finalServerPrerenderStore
.
tags
,
collectedTags
:
finalServerPrerenderStore
.
tags
,
}
}
}
}
...
@@ -3802,7 +3808,7 @@ async function prerenderToStream(
...
@@ -3802,7 +3808,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedStale
:
reactServerPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
reactServerPrerenderStore
.
stale
)
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
}
}
}
else
if
(
fallbackRouteParams
&&
fallbackRouteParams
.
size
>
0
)
{
}
else
if
(
fallbackRouteParams
&&
fallbackRouteParams
.
size
>
0
)
{
...
@@ -3822,7 +3828,7 @@ async function prerenderToStream(
...
@@ -3822,7 +3828,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedStale
:
reactServerPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
reactServerPrerenderStore
.
stale
)
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
}
}
}
else
{
}
else
{
...
@@ -3882,7 +3888,7 @@ async function prerenderToStream(
...
@@ -3882,7 +3888,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedRevalidate
:
reactServerPrerenderStore
.
revalidate
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedExpire
:
reactServerPrerenderStore
.
expire
,
collectedStale
:
reactServerPrerenderStore
.
stale
,
collectedStale
:
selectStaleTime
(
reactServerPrerenderStore
.
stale
)
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
collectedTags
:
reactServerPrerenderStore
.
tags
,
}
}
}
}
...
@@ -3975,7 +3981,7 @@ async function prerenderToStream(
...
@@ -3975,7 +3981,7 @@ async function prerenderToStream(
// TODO: Should this include the SSR pass?
// TODO: Should this include the SSR pass?
collectedRevalidate
:
prerenderLegacyStore
.
revalidate
,
collectedRevalidate
:
prerenderLegacyStore
.
revalidate
,
collectedExpire
:
prerenderLegacyStore
.
expire
,
collectedExpire
:
prerenderLegacyStore
.
expire
,
collectedStale
:
prerenderLegacyStore
.
stale
,
collectedStale
:
selectStaleTime
(
prerenderLegacyStore
.
stale
)
,
collectedTags
:
prerenderLegacyStore
.
tags
,
collectedTags
:
prerenderLegacyStore
.
tags
,
}
}
}
}
...
@@ -4156,8 +4162,9 @@ async function prerenderToStream(
...
@@ -4156,8 +4162,9 @@ async function prerenderToStream(
prerenderStore
!==
null
?
prerenderStore
.
revalidate
:
INFINITE_CACHE
,
prerenderStore
!==
null
?
prerenderStore
.
revalidate
:
INFINITE_CACHE
,
collectedExpire
:
collectedExpire
:
prerenderStore
!==
null
?
prerenderStore
.
expire
:
INFINITE_CACHE
,
prerenderStore
!==
null
?
prerenderStore
.
expire
:
INFINITE_CACHE
,
collectedStale
:
collectedStale
:
selectStaleTime
(
prerenderStore
!==
null
?
prerenderStore
.
stale
:
INFINITE_CACHE
,
prerenderStore
!==
null
?
prerenderStore
.
stale
:
INFINITE_CACHE
),
collectedTags
:
prerenderStore
!==
null
?
prerenderStore
.
tags
:
null
,
collectedTags
:
prerenderStore
!==
null
?
prerenderStore
.
tags
:
null
,
}
}
}
catch
(
finalErr
:
any
)
{
}
catch
(
finalErr
:
any
)
{
...
...
packages/next/src/server/app-render/types.ts
View file @
bda731fa
import
type
{
LoadComponentsReturnType
}
from
'../load-components'
import
type
{
LoadComponentsReturnType
}
from
'../load-components'
import
type
{
ServerRuntime
,
SizeLimit
}
from
'../../types'
import
type
{
ServerRuntime
,
SizeLimit
}
from
'../../types'
import
type
{
NextConfigComplete
}
from
'../../server/config-shared'
import
type
{
ExperimentalConfig
,
NextConfigComplete
,
}
from
'../../server/config-shared'
import
type
{
ClientReferenceManifest
}
from
'../../build/webpack/plugins/flight-manifest-plugin'
import
type
{
ClientReferenceManifest
}
from
'../../build/webpack/plugins/flight-manifest-plugin'
import
type
{
NextFontManifest
}
from
'../../build/webpack/plugins/next-font-manifest-plugin'
import
type
{
NextFontManifest
}
from
'../../build/webpack/plugins/next-font-manifest-plugin'
import
type
{
ParsedUrlQuery
}
from
'querystring'
import
type
{
ParsedUrlQuery
}
from
'querystring'
...
@@ -211,6 +214,7 @@ export interface RenderOptsPartial {
...
@@ -211,6 +214,7 @@ export interface RenderOptsPartial {
*/
*/
isRoutePPREnabled
?:
boolean
isRoutePPREnabled
?:
boolean
expireTime
:
number
|
undefined
expireTime
:
number
|
undefined
staleTimes
:
ExperimentalConfig
[
'staleTimes'
]
|
undefined
clientTraceMetadata
:
string
[]
|
undefined
clientTraceMetadata
:
string
[]
|
undefined
dynamicIO
:
boolean
dynamicIO
:
boolean
clientSegmentCache
:
boolean
|
'client-only'
clientSegmentCache
:
boolean
|
'client-only'
...
...
packages/next/src/server/base-server.ts
View file @
bda731fa
...
@@ -601,6 +601,7 @@ export default abstract class Server<
...
@@ -601,6 +601,7 @@ export default abstract class Server<
htmlLimitedBots
:
this
.
nextConfig
.
htmlLimitedBots
,
htmlLimitedBots
:
this
.
nextConfig
.
htmlLimitedBots
,
experimental
:
{
experimental
:
{
expireTime
:
this
.
nextConfig
.
expireTime
,
expireTime
:
this
.
nextConfig
.
expireTime
,
staleTimes
:
this
.
nextConfig
.
experimental
.
staleTimes
,
clientTraceMetadata
:
this
.
nextConfig
.
experimental
.
clientTraceMetadata
,
clientTraceMetadata
:
this
.
nextConfig
.
experimental
.
clientTraceMetadata
,
dynamicIO
:
this
.
nextConfig
.
experimental
.
dynamicIO
??
false
,
dynamicIO
:
this
.
nextConfig
.
experimental
.
dynamicIO
??
false
,
clientSegmentCache
:
clientSegmentCache
:
...
...
test/e2e/app-dir/app-prefetch/prefetching.stale-times.test.ts
View file @
bda731fa
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
{
retry
,
waitFor
}
from
'next-test-utils'
import
{
NEXT_RSC_UNION_QUERY
}
from
'next/dist/client/components/app-router-headers'
const
browserConfigWithFixedTime
=
{
beforePageLoad
:
(
page
)
=>
{
page
.
addInitScript
(()
=>
{
const
startTime
=
new
Date
()
const
fixedTime
=
new
Date
(
'2023-04-17T00:00:00Z'
)
// Override the Date constructor
// @ts-ignore
// eslint-disable-next-line no-native-reassign
Date
=
class
extends
Date
{
constructor
()
{
super
()
// @ts-ignore
return
new
startTime
.
constructor
(
fixedTime
)
}
static
now
()
{
return
fixedTime
.
getTime
()
}
}
})
},
}
describe
(
'app dir - prefetching (custom staleTime)'
,
()
=>
{
describe
(
'app dir - prefetching (custom staleTime)'
,
()
=>
{
const
{
next
,
isNextDev
}
=
nextTestSetup
({
const
{
next
,
isNextDev
}
=
nextTestSetup
({
...
@@ -34,8 +8,8 @@ describe('app dir - prefetching (custom staleTime)', () => {
...
@@ -34,8 +8,8 @@ describe('app dir - prefetching (custom staleTime)', () => {
nextConfig
:
{
nextConfig
:
{
experimental
:
{
experimental
:
{
staleTimes
:
{
staleTimes
:
{
static
:
18
0
,
static
:
3
0
,
dynamic
:
3
0
,
dynamic
:
2
0
,
},
},
},
},
},
},
...
@@ -47,7 +21,7 @@ describe('app dir - prefetching (custom staleTime)', () => {
...
@@ -47,7 +21,7 @@ describe('app dir - prefetching (custom staleTime)', () => {
}
}
it
(
'should not fetch again when a static page was prefetched when navigating to it twice'
,
async
()
=>
{
it
(
'should not fetch again when a static page was prefetched when navigating to it twice'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/404'
,
browserConfigWithFixedTime
)
const
browser
=
await
next
.
browser
(
'/404'
)
let
requests
:
string
[]
=
[]
let
requests
:
string
[]
=
[]
browser
.
on
(
'request'
,
(
req
)
=>
{
browser
.
on
(
'request'
,
(
req
)
=>
{
...
@@ -55,17 +29,10 @@ describe('app dir - prefetching (custom staleTime)', () => {
...
@@ -55,17 +29,10 @@ describe('app dir - prefetching (custom staleTime)', () => {
})
})
await
browser
.
eval
(
'location.href = "/"'
)
await
browser
.
eval
(
'location.href = "/"'
)
await
browser
.
eval
(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await
retry
(
async
()
=>
{
await
retry
(
async
()
=>
{
expect
(
expect
(
requests
.
filter
(
requests
.
filter
((
request
)
=>
request
===
'/static-page'
)
(
request
)
=>
).
toHaveLength
(
1
)
request
===
'/static-page'
||
request
.
includes
(
NEXT_RSC_UNION_QUERY
)
).
length
).
toBe
(
1
)
})
})
await
browser
await
browser
...
@@ -86,11 +53,49 @@ describe('app dir - prefetching (custom staleTime)', () => {
...
@@ -86,11 +53,49 @@ describe('app dir - prefetching (custom staleTime)', () => {
await
retry
(
async
()
=>
{
await
retry
(
async
()
=>
{
expect
(
expect
(
requests
.
filter
(
requests
.
filter
((
request
)
=>
request
===
'/static-page'
)
(
request
)
=>
).
toHaveLength
(
1
)
request
===
'/static-page'
||
request
.
includes
(
NEXT_RSC_UNION_QUERY
)
})
).
length
})
).
toBe
(
1
)
it
(
'should fetch again when a static page was prefetched when navigating to it after the stale time has passed'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/404'
)
let
requests
:
string
[]
=
[]
browser
.
on
(
'request'
,
(
req
)
=>
{
requests
.
push
(
new
URL
(
req
.
url
()).
pathname
)
})
await
browser
.
eval
(
'location.href = "/"'
)
await
retry
(
async
()
=>
{
expect
(
requests
.
filter
((
request
)
=>
request
===
'/static-page'
)
).
toHaveLength
(
1
)
})
await
browser
.
elementByCss
(
'#to-static-page'
)
.
click
()
.
waitForElementByCss
(
'#static-page'
)
const
linkToStaticPage
=
await
browser
.
elementByCss
(
'#to-home'
)
// Go back to home page
.
click
()
// Wait for homepage to load
.
waitForElementByCss
(
'#to-static-page'
)
// Wait for the stale time to pass.
await
waitFor
(
30000
)
// Click on the link to the static page again
await
linkToStaticPage
.
click
()
// Wait for the static page to load again
await
browser
.
waitForElementByCss
(
'#static-page'
)
await
retry
(
async
()
=>
{
expect
(
requests
.
filter
((
request
)
=>
request
===
'/static-page'
)
).
toHaveLength
(
2
)
})
})
})
})
...
...
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