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
3713e8ac
Commit
3713e8ac
authored
Apr 11, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reuse fallback segment base for optimistic routes
parent
9553f4cb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
12 deletions
+79
-12
cache.test.ts
...es/next/src/client/components/segment-cache/cache.test.ts
+43
-0
cache.ts
packages/next/src/client/components/segment-cache/cache.ts
+35
-12
optimistic-routes.ts
.../src/client/components/segment-cache/optimistic-routes.ts
+1
-0
No files found.
packages/next/src/client/components/segment-cache/cache.test.ts
0 → 100644
View file @
3713e8ac
import
{
HEAD_REQUEST_KEY
,
type
SegmentRequestKey
,
}
from
'../../../shared/lib/segment-cache/segment-value-encoding'
import
{
getOutputExportSegmentRequestUrl
}
from
'./cache'
describe
(
'getOutputExportSegmentRequestUrl'
,
()
=>
{
it
(
'uses the concrete rendered route when no fallback base path is known'
,
()
=>
{
expect
(
getOutputExportSegmentRequestUrl
(
new
URL
(
'https://example.com/org/acme/chat/thread-456/?mode=full'
),
HEAD_REQUEST_KEY
,
null
).
href
).
toBe
(
'https://example.com/org/acme/chat/thread-456/__next._head.txt?mode=full'
)
})
it
(
'reuses the learned fallback base path for sibling segment requests'
,
()
=>
{
expect
(
getOutputExportSegmentRequestUrl
(
new
URL
(
'https://example.com/org/acme/chat/thread-456/?mode=full'
),
'/t/$d$threadId'
as
SegmentRequestKey
,
'/org/__fallback'
).
href
).
toBe
(
'https://example.com/org/__fallback/__next.t.$d$threadId.txt?mode=full'
)
})
it
(
'keeps the matched branch fallback base path for conflicting routes'
,
()
=>
{
expect
(
getOutputExportSegmentRequestUrl
(
new
URL
(
'https://example.com/docs/api/reference'
),
'/docs/$d$section/$d$page'
as
SegmentRequestKey
,
'/docs/__fallback/__route_0'
).
href
).
toBe
(
'https://example.com/docs/__fallback/__route_0/__next.docs.$d$section.$d$page.txt'
)
})
})
packages/next/src/client/components/segment-cache/cache.ts
View file @
3713e8ac
...
...
@@ -230,6 +230,7 @@ export type FulfilledRouteCacheEntry = RouteCacheEntryShared & {
metadata
:
RouteTree
supportsPerSegmentPrefetching
:
boolean
hasInlinedSegments
:
boolean
outputExportFallbackBasePath
:
string
|
null
// When true, this entry should not be used as a template for route
// prediction. Set when we discover that the URL was rewritten by middleware
// to a different route structure (e.g., /foo was rewritten to /bar). Since
...
...
@@ -695,6 +696,8 @@ export function deprecated_requestOptimisticRouteCacheEntry(
supportsPerSegmentPrefetching
:
routeWithNoSearchParams
.
supportsPerSegmentPrefetching
,
hasInlinedSegments
:
routeWithNoSearchParams
.
hasInlinedSegments
,
outputExportFallbackBasePath
:
routeWithNoSearchParams
.
outputExportFallbackBasePath
,
hasDynamicRewrite
:
routeWithNoSearchParams
.
hasDynamicRewrite
,
// Override the rendered search with the optimistic value.
...
...
@@ -1116,6 +1119,7 @@ export function fulfillRouteCacheEntry(
fulfilledEntry
.
renderedSearch
=
renderedSearch
fulfilledEntry
.
supportsPerSegmentPrefetching
=
supportsPerSegmentPrefetching
fulfilledEntry
.
hasInlinedSegments
=
false
fulfilledEntry
.
outputExportFallbackBasePath
=
null
fulfilledEntry
.
hasDynamicRewrite
=
false
pingBlockedTasks
(
entry
)
return
fulfilledEntry
...
...
@@ -1912,6 +1916,7 @@ type OutputExportFallbackNavigationData = {
couldBeIntercepted
:
boolean
flightDatas
:
NormalizedFlightData
[]
headVaryParams
:
VaryParams
|
null
outputExportFallbackBasePath
:
string
responseSize
:
number
staleAt
:
number
supportsPerSegmentPrefetching
:
boolean
...
...
@@ -1977,6 +1982,7 @@ async function fetchOutputExportFallbackNavigationData(
couldBeIntercepted
:
serverData
.
i
,
flightDatas
,
headVaryParams
,
outputExportFallbackBasePath
:
fallbackResult
.
fallbackUrl
.
pathname
,
responseSize
,
staleAt
:
await
getStaleAt
(
now
,
serverData
.
s
),
supportsPerSegmentPrefetching
:
serverData
.
S
,
...
...
@@ -2007,6 +2013,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
couldBeIntercepted
,
flightDatas
,
headVaryParams
,
outputExportFallbackBasePath
,
responseSize
,
staleAt
,
supportsPerSegmentPrefetching
,
...
...
@@ -2054,6 +2061,7 @@ async function fetchRouteOnCacheMissFromOutputExportFallback(
false
)
fulfilledEntry
.
hasInlinedSegments
=
true
fulfilledEntry
.
outputExportFallbackBasePath
=
outputExportFallbackBasePath
if
(
!
couldBeIntercepted
)
{
const
fulfilledVaryPath
=
getFulfilledRouteVaryPath
(
...
...
@@ -2200,10 +2208,14 @@ export async function fetchSegmentsOnCacheMiss(
// Testing API. Static pre-renders don't normally happen during development.
addInstantPrefetchHeaderIfLocked
(
headers
)
const
requestUrl
=
isOutputExportMode
?
// In output: "export" mode, we need to add the segment path to the URL.
addSegmentPathToUrlInOutputExportMode
(
url
,
normalizedRequestKey
)
:
url
let
requestUrl
=
url
if
(
isOutputExportMode
)
{
requestUrl
=
getOutputExportSegmentRequestUrl
(
url
,
normalizedRequestKey
,
route
.
outputExportFallbackBasePath
)
}
try
{
const
response
=
await
fetchPrefetchResponse
(
requestUrl
,
headers
)
if
(
...
...
@@ -3100,6 +3112,24 @@ function createIncrementalPrefetchResponseStream(
})
}
export
function
getOutputExportSegmentRequestUrl
(
url
:
URL
,
segmentPath
:
SegmentRequestKey
,
outputExportFallbackBasePath
:
string
|
null
):
URL
{
const
staticUrl
=
new
URL
(
url
)
if
(
outputExportFallbackBasePath
!==
null
)
{
staticUrl
.
pathname
=
outputExportFallbackBasePath
}
const
routeDir
=
staticUrl
.
pathname
.
endsWith
(
'/'
)
?
staticUrl
.
pathname
.
slice
(
0
,
-
1
)
:
staticUrl
.
pathname
const
staticExportFilename
=
convertSegmentPathToStaticExportFilename
(
segmentPath
)
staticUrl
.
pathname
=
`
${
routeDir
}
/
${
staticExportFilename
}
`
return
staticUrl
}
function
addSegmentPathToUrlInOutputExportMode
(
url
:
URL
,
segmentPath
:
SegmentRequestKey
...
...
@@ -3107,14 +3137,7 @@ function addSegmentPathToUrlInOutputExportMode(
if
(
isOutputExportMode
)
{
// In output: "export" mode, we cannot use a header to encode the segment
// path. Instead, we append it to the end of the pathname.
const
staticUrl
=
new
URL
(
url
)
const
routeDir
=
staticUrl
.
pathname
.
endsWith
(
'/'
)
?
staticUrl
.
pathname
.
slice
(
0
,
-
1
)
:
staticUrl
.
pathname
const
staticExportFilename
=
convertSegmentPathToStaticExportFilename
(
segmentPath
)
staticUrl
.
pathname
=
`
${
routeDir
}
/
${
staticExportFilename
}
`
return
staticUrl
return
getOutputExportSegmentRequestUrl
(
url
,
segmentPath
,
null
)
}
return
url
}
...
...
packages/next/src/client/components/segment-cache/optimistic-routes.ts
View file @
3713e8ac
...
...
@@ -588,6 +588,7 @@ export function matchKnownRoute(
couldBeIntercepted
:
pattern
.
couldBeIntercepted
,
supportsPerSegmentPrefetching
:
pattern
.
supportsPerSegmentPrefetching
,
hasInlinedSegments
:
pattern
.
hasInlinedSegments
,
outputExportFallbackBasePath
:
pattern
.
outputExportFallbackBasePath
,
hasDynamicRewrite
:
false
,
renderedSearch
:
search
,
ref
:
null
,
...
...
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