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
616f6fa0
Unverified
Commit
616f6fa0
authored
Jun 16, 2025
by
Jiachi Liu
Committed by
GitHub
Jun 16, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[backport][metadata] render streaming metadata on the top level (#80566)
parent
3ab8db73
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
40 additions
and
113 deletions
+40
-113
find-head-in-cache.ts
.../components/router-reducer/reducers/find-head-in-cache.ts
+8
-19
metadata.tsx
packages/next/src/lib/metadata/metadata.tsx
+5
-3
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+13
-63
create-component-tree.tsx
...ages/next/src/server/app-render/create-component-tree.tsx
+0
-11
walk-tree-with-flight-router-state.tsx
.../server/app-render/walk-tree-with-flight-router-state.tsx
+0
-1
page.js
test/e2e/esm-externals/app/client/page.js
+2
-2
page.js
test/e2e/esm-externals/app/server/page.js
+2
-2
esm-externals.test.ts
test/e2e/esm-externals/esm-externals.test.ts
+4
-6
ssg.js
test/e2e/esm-externals/pages/ssg.js
+2
-2
ssr.js
test/e2e/esm-externals/pages/ssr.js
+2
-2
static.js
test/e2e/esm-externals/pages/static.js
+2
-2
No files found.
packages/next/src/client/components/router-reducer/reducers/find-head-in-cache.ts
View file @
616f6fa0
...
@@ -22,27 +22,16 @@ function findHeadInCacheImpl(
...
@@ -22,27 +22,16 @@ function findHeadInCacheImpl(
// First try the 'children' parallel route if it exists
// First try the 'children' parallel route if it exists
// when starting from the "root", this corresponds with the main page component
// when starting from the "root", this corresponds with the main page component
if
(
parallelRoutes
.
children
)
{
const
parallelRoutesKeys
=
Object
.
keys
(
parallelRoutes
).
filter
(
const
[
segment
,
childParallelRoutes
]
=
parallelRoutes
.
children
(
key
)
=>
key
!==
'children'
const
childSegmentMap
=
cache
.
parallelRoutes
.
get
(
'children'
)
)
if
(
childSegmentMap
)
{
const
cacheKey
=
createRouterCacheKey
(
segment
)
const
cacheNode
=
childSegmentMap
.
get
(
cacheKey
)
if
(
cacheNode
)
{
const
item
=
findHeadInCacheImpl
(
cacheNode
,
childParallelRoutes
,
keyPrefix
+
'/'
+
cacheKey
)
if
(
item
)
return
item
}
}
}
// if we didn't find metadata in the page slot, check the other parallel routes
// if we are at the root, we need to check the children slot first
for
(
const
key
in
parallelRoutes
)
{
if
(
'children'
in
parallelRoutes
)
{
if
(
key
===
'children'
)
continue
// already checked above
parallelRoutesKeys
.
unshift
(
'children'
)
}
for
(
const
key
of
parallelRoutesKeys
)
{
const
[
segment
,
childParallelRoutes
]
=
parallelRoutes
[
key
]
const
[
segment
,
childParallelRoutes
]
=
parallelRoutes
[
key
]
const
childSegmentMap
=
cache
.
parallelRoutes
.
get
(
key
)
const
childSegmentMap
=
cache
.
parallelRoutes
.
get
(
key
)
if
(
!
childSegmentMap
)
{
if
(
!
childSegmentMap
)
{
...
...
packages/next/src/lib/metadata/metadata.tsx
View file @
616f6fa0
...
@@ -206,9 +206,11 @@ export function createMetadataComponents({
...
@@ -206,9 +206,11 @@ export function createMetadataComponents({
const
promise
=
resolveFinalMetadata
()
const
promise
=
resolveFinalMetadata
()
if
(
serveStreamingMetadata
)
{
if
(
serveStreamingMetadata
)
{
return
(
return
(
<
Suspense
fallback=
{
null
}
>
<
div
hidden
>
<
AsyncMetadata
promise=
{
promise
}
/>
<
Suspense
fallback=
{
null
}
>
</
Suspense
>
<
AsyncMetadata
promise=
{
promise
}
/>
</
Suspense
>
</
div
>
)
)
}
}
const
metadataState
=
await
promise
const
metadataState
=
await
promise
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
616f6fa0
...
@@ -242,6 +242,8 @@ interface ParseRequestHeadersOptions {
...
@@ -242,6 +242,8 @@ interface ParseRequestHeadersOptions {
}
}
const
flightDataPathHeadKey
=
'h'
const
flightDataPathHeadKey
=
'h'
const
getFlightViewportKey
=
(
requestId
:
string
)
=>
requestId
+
'v'
const
getFlightMetadataKey
=
(
requestId
:
string
)
=>
requestId
+
'm'
interface
ParsedRequestHeaders
{
interface
ParsedRequestHeaders
{
/**
/**
...
@@ -333,30 +335,6 @@ function createNotFoundLoaderTree(loaderTree: LoaderTree): LoaderTree {
...
@@ -333,30 +335,6 @@ function createNotFoundLoaderTree(loaderTree: LoaderTree): LoaderTree {
]
]
}
}
function
createDivergedMetadataComponents
(
Metadata
:
React
.
ComponentType
,
serveStreamingMetadata
:
boolean
):
{
StaticMetadata
:
React
.
ComponentType
<
{}
>
StreamingMetadata
:
React
.
ComponentType
<
{}
>
|
null
}
{
function
EmptyMetadata
()
{
return
null
}
const
StreamingMetadata
:
React
.
ComponentType
|
null
=
serveStreamingMetadata
?
Metadata
:
null
const
StaticMetadata
:
React
.
ComponentType
<
{}
>
=
serveStreamingMetadata
?
EmptyMetadata
:
Metadata
return
{
StaticMetadata
,
StreamingMetadata
,
}
}
/**
/**
* Returns a function that parses the dynamic segment and return the associated value.
* Returns a function that parses the dynamic segment and return the associated value.
*/
*/
...
@@ -524,14 +502,6 @@ async function generateDynamicRSCPayload(
...
@@ -524,14 +502,6 @@ async function generateDynamicRSCPayload(
serveStreamingMetadata
,
serveStreamingMetadata
,
})
})
const
{
StreamingMetadata
,
StaticMetadata
}
=
createDivergedMetadataComponents
(()
=>
{
return
(
// Adding requestId as react key to make metadata remount for each render
<
MetadataTree
key=
{
requestId
}
/>
)
},
serveStreamingMetadata
)
flightData
=
(
flightData
=
(
await
walkTreeWithFlightRouterState
({
await
walkTreeWithFlightRouterState
({
ctx
,
ctx
,
...
@@ -548,9 +518,9 @@ async function generateDynamicRSCPayload(
...
@@ -548,9 +518,9 @@ async function generateDynamicRSCPayload(
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
/>
/>
{
/* Adding requestId as react key to make metadata remount for each render */
}
{
/* Adding requestId as react key to make metadata remount for each render */
}
<
ViewportTree
key=
{
requestId
}
/>
<
ViewportTree
key=
{
getFlightViewportKey
(
requestId
)
}
/>
{
StreamingMetadata
?
<
StreamingMetadata
/>
:
null
}
{
/* Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed */
}
<
StaticMetadata
/>
<
MetadataTree
key=
{
getFlightMetadataKey
(
requestId
)
}
/>
</
React
.
Fragment
>
</
React
.
Fragment
>
),
),
injectedCSS
:
new
Set
(),
injectedCSS
:
new
Set
(),
...
@@ -854,14 +824,6 @@ async function getRSCPayload(
...
@@ -854,14 +824,6 @@ async function getRSCPayload(
const
preloadCallbacks
:
PreloadCallbacks
=
[]
const
preloadCallbacks
:
PreloadCallbacks
=
[]
const
{
StreamingMetadata
,
StaticMetadata
}
=
createDivergedMetadataComponents
(()
=>
{
return
(
// Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed
<
MetadataTree
/>
)
},
serveStreamingMetadata
)
const
seedData
=
await
createComponentTree
({
const
seedData
=
await
createComponentTree
({
ctx
,
ctx
,
loaderTree
:
tree
,
loaderTree
:
tree
,
...
@@ -875,7 +837,6 @@ async function getRSCPayload(
...
@@ -875,7 +837,6 @@ async function getRSCPayload(
missingSlots
,
missingSlots
,
preloadCallbacks
,
preloadCallbacks
,
authInterrupts
:
ctx
.
renderOpts
.
experimental
.
authInterrupts
,
authInterrupts
:
ctx
.
renderOpts
.
experimental
.
authInterrupts
,
StreamingMetadata
,
StreamingMetadataOutlet
,
StreamingMetadataOutlet
,
})
})
...
@@ -893,8 +854,9 @@ async function getRSCPayload(
...
@@ -893,8 +854,9 @@ async function getRSCPayload(
statusCode=
{
ctx
.
res
.
statusCode
}
statusCode=
{
ctx
.
res
.
statusCode
}
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
/>
/>
<
ViewportTree
key=
{
ctx
.
requestId
}
/>
<
ViewportTree
key=
{
getFlightViewportKey
(
ctx
.
requestId
)
}
/>
<
StaticMetadata
/>
{
/* Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed */
}
<
MetadataTree
/>
</
React
.
Fragment
>
</
React
.
Fragment
>
)
)
...
@@ -981,16 +943,8 @@ async function getErrorRSCPayload(
...
@@ -981,16 +943,8 @@ async function getErrorRSCPayload(
serveStreamingMetadata
:
serveStreamingMetadata
,
serveStreamingMetadata
:
serveStreamingMetadata
,
})
})
const
{
StreamingMetadata
,
StaticMetadata
}
=
// {/* Adding requestId as react key to make metadata remount for each render */}
createDivergedMetadataComponents
(
const
metadata
=
<
MetadataTree
key=
{
getFlightMetadataKey
(
requestId
)
}
/>
()
=>
(
<
React
.
Fragment
key=
{
flightDataPathHeadKey
}
>
{
/* Adding requestId as react key to make metadata remount for each render */
}
<
MetadataTree
key=
{
requestId
}
/>
</
React
.
Fragment
>
),
serveStreamingMetadata
)
const
initialHead
=
(
const
initialHead
=
(
<
React
.
Fragment
key=
{
flightDataPathHeadKey
}
>
<
React
.
Fragment
key=
{
flightDataPathHeadKey
}
>
...
@@ -1000,12 +954,11 @@ async function getErrorRSCPayload(
...
@@ -1000,12 +954,11 @@ async function getErrorRSCPayload(
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
isPossibleServerAction=
{
ctx
.
isPossibleServerAction
}
/>
/>
{
/* Adding requestId as react key to make metadata remount for each render */
}
{
/* Adding requestId as react key to make metadata remount for each render */
}
<
ViewportTree
key=
{
requestId
}
/>
<
ViewportTree
key=
{
getFlightViewportKey
(
requestId
)
}
/>
{
process
.
env
.
NODE_ENV
===
'development'
&&
(
{
process
.
env
.
NODE_ENV
===
'development'
&&
(
<
meta
name=
"next-error"
content=
"not-found"
/>
<
meta
name=
"next-error"
content=
"not-found"
/>
)
}
)
}
{
StreamingMetadata
?
<
StreamingMetadata
/>
:
null
}
{
metadata
}
<
StaticMetadata
/>
</
React
.
Fragment
>
</
React
.
Fragment
>
)
)
...
@@ -1025,10 +978,7 @@ async function getErrorRSCPayload(
...
@@ -1025,10 +978,7 @@ async function getErrorRSCPayload(
const
seedData
:
CacheNodeSeedData
=
[
const
seedData
:
CacheNodeSeedData
=
[
initialTree
[
0
],
initialTree
[
0
],
<
html
id=
"__next_error__"
>
<
html
id=
"__next_error__"
>
<
head
>
<
head
>
{
metadata
}
</
head
>
{
StreamingMetadata
?
<
StreamingMetadata
/>
:
null
}
<
StaticMetadata
/>
</
head
>
<
body
>
<
body
>
{
process
.
env
.
NODE_ENV
!==
'production'
&&
err
?
(
{
process
.
env
.
NODE_ENV
!==
'production'
&&
err
?
(
<
template
<
template
...
...
packages/next/src/server/app-render/create-component-tree.tsx
View file @
616f6fa0
...
@@ -39,7 +39,6 @@ export function createComponentTree(props: {
...
@@ -39,7 +39,6 @@ export function createComponentTree(props: {
missingSlots
?:
Set
<
string
>
missingSlots
?:
Set
<
string
>
preloadCallbacks
:
PreloadCallbacks
preloadCallbacks
:
PreloadCallbacks
authInterrupts
:
boolean
authInterrupts
:
boolean
StreamingMetadata
:
React
.
ComponentType
|
null
StreamingMetadataOutlet
:
React
.
ComponentType
StreamingMetadataOutlet
:
React
.
ComponentType
}):
Promise
<
CacheNodeSeedData
>
{
}):
Promise
<
CacheNodeSeedData
>
{
return
getTracer
().
trace
(
return
getTracer
().
trace
(
...
@@ -76,7 +75,6 @@ async function createComponentTreeInternal({
...
@@ -76,7 +75,6 @@ async function createComponentTreeInternal({
missingSlots
,
missingSlots
,
preloadCallbacks
,
preloadCallbacks
,
authInterrupts
,
authInterrupts
,
StreamingMetadata
,
StreamingMetadataOutlet
,
StreamingMetadataOutlet
,
}:
{
}:
{
loaderTree
:
LoaderTree
loaderTree
:
LoaderTree
...
@@ -91,7 +89,6 @@ async function createComponentTreeInternal({
...
@@ -91,7 +89,6 @@ async function createComponentTreeInternal({
missingSlots
?:
Set
<
string
>
missingSlots
?:
Set
<
string
>
preloadCallbacks
:
PreloadCallbacks
preloadCallbacks
:
PreloadCallbacks
authInterrupts
:
boolean
authInterrupts
:
boolean
StreamingMetadata
:
React
.
ComponentType
|
null
StreamingMetadataOutlet
:
React
.
ComponentType
|
null
StreamingMetadataOutlet
:
React
.
ComponentType
|
null
}):
Promise
<
CacheNodeSeedData
>
{
}):
Promise
<
CacheNodeSeedData
>
{
const
{
const
{
...
@@ -388,7 +385,6 @@ async function createComponentTreeInternal({
...
@@ -388,7 +385,6 @@ async function createComponentTreeInternal({
// Resolve the segment param
// Resolve the segment param
const
actualSegment
=
segmentParam
?
segmentParam
.
treeSegment
:
segment
const
actualSegment
=
segmentParam
?
segmentParam
.
treeSegment
:
segment
const
metadata
=
StreamingMetadata
?
<
StreamingMetadata
/>
:
undefined
// Use the same condition to render metadataOutlet as metadata
// Use the same condition to render metadataOutlet as metadata
const
metadataOutlet
=
StreamingMetadataOutlet
?
(
const
metadataOutlet
=
StreamingMetadataOutlet
?
(
...
@@ -511,7 +507,6 @@ async function createComponentTreeInternal({
...
@@ -511,7 +507,6 @@ async function createComponentTreeInternal({
missingSlots
,
missingSlots
,
preloadCallbacks
,
preloadCallbacks
,
authInterrupts
,
authInterrupts
,
StreamingMetadata
:
isChildrenRouteKey
?
StreamingMetadata
:
null
,
// `StreamingMetadataOutlet` is used to conditionally throw. In the case of parallel routes we will have more than one page
// `StreamingMetadataOutlet` is used to conditionally throw. In the case of parallel routes we will have more than one page
// but we only want to throw on the first one.
// but we only want to throw on the first one.
StreamingMetadataOutlet
:
isChildrenRouteKey
StreamingMetadataOutlet
:
isChildrenRouteKey
...
@@ -694,12 +689,6 @@ async function createComponentTreeInternal({
...
@@ -694,12 +689,6 @@ async function createComponentTreeInternal({
actualSegment
,
actualSegment
,
<
React
.
Fragment
key=
{
cacheNodeKey
}
>
<
React
.
Fragment
key=
{
cacheNodeKey
}
>
{
pageElement
}
{
pageElement
}
{
/*
* The order here matters since a parent might call findDOMNode().
* findDOMNode() will return the first child if multiple children are rendered.
* But React will hoist metadata into <head> which breaks scroll handling.
*/
}
{
metadata
}
{
layerAssets
}
{
layerAssets
}
<
OutletBoundary
>
<
OutletBoundary
>
<
MetadataOutlet
ready=
{
getViewportReady
}
/>
<
MetadataOutlet
ready=
{
getViewportReady
}
/>
...
...
packages/next/src/server/app-render/walk-tree-with-flight-router-state.tsx
View file @
616f6fa0
...
@@ -202,7 +202,6 @@ export async function walkTreeWithFlightRouterState({
...
@@ -202,7 +202,6 @@ export async function walkTreeWithFlightRouterState({
getMetadataReady
,
getMetadataReady
,
preloadCallbacks
,
preloadCallbacks
,
authInterrupts
:
experimental
.
authInterrupts
,
authInterrupts
:
experimental
.
authInterrupts
,
StreamingMetadata
:
null
,
StreamingMetadataOutlet
,
StreamingMetadataOutlet
,
}
}
)
)
...
...
test/e2e/esm-externals/app/client/page.js
View file @
616f6fa0
...
@@ -6,8 +6,8 @@ import World3 from 'app-cjs-esm-package/entry'
...
@@ -6,8 +6,8 @@ import World3 from 'app-cjs-esm-package/entry'
export
default
function
Index
()
{
export
default
function
Index
()
{
return
(
return
(
<
div
>
<
p
>
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
<
/
div
>
<
/
p
>
)
)
}
}
test/e2e/esm-externals/app/server/page.js
View file @
616f6fa0
...
@@ -4,8 +4,8 @@ import World3 from 'app-cjs-esm-package/entry'
...
@@ -4,8 +4,8 @@ import World3 from 'app-cjs-esm-package/entry'
export
default
function
Page
()
{
export
default
function
Page
()
{
return
(
return
(
<
div
>
<
p
>
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
<
/
div
>
<
/
p
>
)
)
}
}
test/e2e/esm-externals/esm-externals.test.ts
View file @
616f6fa0
...
@@ -28,15 +28,13 @@ describe('esm-externals', () => {
...
@@ -28,15 +28,13 @@ describe('esm-externals', () => {
it
(
'should return the correct SSR HTML'
,
async
()
=>
{
it
(
'should return the correct SSR HTML'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
url
)
const
$
=
await
next
.
render$
(
url
)
const
body
=
$
(
'body
> div > div
'
).
html
()
const
body
=
$
(
'body
p
'
).
html
()
expect
(
normalize
(
body
)).
toEqual
(
expectedHtml
)
expect
(
normalize
(
body
)).
toEqual
(
expectedHtml
)
})
})
it
(
'should render the correct page'
,
async
()
=>
{
it
(
'should render the correct page'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
url
)
const
browser
=
await
next
.
browser
(
url
)
expect
(
await
browser
.
elementByCss
(
'body > div'
).
text
()).
toEqual
(
expect
(
await
browser
.
elementByCss
(
'body p'
).
text
()).
toEqual
(
expectedText
)
expectedText
)
})
})
})
})
...
@@ -54,13 +52,13 @@ describe('esm-externals', () => {
...
@@ -54,13 +52,13 @@ describe('esm-externals', () => {
it
(
'should return the correct SSR HTML'
,
async
()
=>
{
it
(
'should return the correct SSR HTML'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
url
)
const
$
=
await
next
.
render$
(
url
)
const
body
=
$
(
'body >
div
'
).
html
()
const
body
=
$
(
'body >
p
'
).
html
()
expect
(
normalize
(
body
)).
toEqual
(
expectedHtml
)
expect
(
normalize
(
body
)).
toEqual
(
expectedHtml
)
})
})
it
(
'should render the correct page'
,
async
()
=>
{
it
(
'should render the correct page'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
url
)
const
browser
=
await
next
.
browser
(
url
)
expect
(
await
browser
.
elementByCss
(
'body >
div
'
).
text
()).
toEqual
(
expect
(
await
browser
.
elementByCss
(
'body >
p
'
).
text
()).
toEqual
(
expectedText
expectedText
)
)
})
})
...
...
test/e2e/esm-externals/pages/ssg.js
View file @
616f6fa0
...
@@ -13,8 +13,8 @@ export async function getStaticProps() {
...
@@ -13,8 +13,8 @@ export async function getStaticProps() {
export
default
function
Index
({
worlds
})
{
export
default
function
Index
({
worlds
})
{
return
(
return
(
<
div
>
<
p
>
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
<
/
div
>
<
/
p
>
)
)
}
}
test/e2e/esm-externals/pages/ssr.js
View file @
616f6fa0
...
@@ -13,8 +13,8 @@ export function getServerSideProps() {
...
@@ -13,8 +13,8 @@ export function getServerSideProps() {
export
default
function
Index
({
worlds
})
{
export
default
function
Index
({
worlds
})
{
return
(
return
(
<
div
>
<
p
>
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
<
/
div
>
<
/
p
>
)
)
}
}
test/e2e/esm-externals/pages/static.js
View file @
616f6fa0
...
@@ -7,8 +7,8 @@ const worlds = 'World+World+World'
...
@@ -7,8 +7,8 @@ const worlds = 'World+World+World'
export
default
function
Index
()
{
export
default
function
Index
()
{
return
(
return
(
<
div
>
<
p
>
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
Hello
{
World1
}
+
{
World2
}
+
{
World3
}
+
{
worlds
}
<
/
div
>
<
/
p
>
)
)
}
}
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