Skip to content

Commit a881e56

Browse files
committed
refactor and ensure keys are set
1 parent de1be75 commit a881e56

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

packages/next/src/client/components/router-reducer/reducers/find-head-in-cache.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ function findHeadInCacheImpl(
2020
return [cache, keyPrefix]
2121
}
2222

23-
for (const key in parallelRoutes) {
23+
// First try the 'children' parallel route if it exists
24+
// when starting from the "root", this corresponds with the main page component
25+
const parallelRoutesKeys = Object.keys(parallelRoutes).filter(
26+
(key) => key !== 'children'
27+
)
28+
29+
// if we are at the root, we need to check the children slot first
30+
if ('children' in parallelRoutes) {
31+
parallelRoutesKeys.unshift('children')
32+
}
33+
34+
// if we didn't find metadata in the page slot, check the other parallel routes
35+
for (const key of parallelRoutesKeys) {
2436
const [segment, childParallelRoutes] = parallelRoutes[key]
2537
const childSegmentMap = cache.parallelRoutes.get(key)
2638
if (!childSegmentMap) {

packages/next/src/server/app-render/app-render.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ interface ParseRequestHeadersOptions {
242242
}
243243

244244
const flightDataPathHeadKey = 'h'
245+
const getFlightViewportKey = (requestId: string) => requestId + 'v'
246+
const getFlightMetadataKey = (requestId: string) => requestId + 'm'
245247

246248
interface ParsedRequestHeaders {
247249
/**
@@ -516,9 +518,9 @@ async function generateDynamicRSCPayload(
516518
isPossibleServerAction={ctx.isPossibleServerAction}
517519
/>
518520
{/* Adding requestId as react key to make metadata remount for each render */}
519-
<ViewportTree key={requestId} />
521+
<ViewportTree key={getFlightViewportKey(requestId)} />
520522
{/* Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed */}
521-
<MetadataTree />
523+
<MetadataTree key={getFlightMetadataKey(requestId)} />
522524
</React.Fragment>
523525
),
524526
injectedCSS: new Set(),
@@ -852,7 +854,7 @@ async function getRSCPayload(
852854
statusCode={ctx.res.statusCode}
853855
isPossibleServerAction={ctx.isPossibleServerAction}
854856
/>
855-
<ViewportTree key={ctx.requestId} />
857+
<ViewportTree key={getFlightViewportKey(ctx.requestId)} />
856858
{/* Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed */}
857859
<MetadataTree />
858860
</React.Fragment>
@@ -941,12 +943,8 @@ async function getErrorRSCPayload(
941943
serveStreamingMetadata: serveStreamingMetadata,
942944
})
943945

944-
const metadata = (
945-
<React.Fragment key={flightDataPathHeadKey}>
946-
{/* Adding requestId as react key to make metadata remount for each render */}
947-
<MetadataTree key={requestId} />
948-
</React.Fragment>
949-
)
946+
// {/* Adding requestId as react key to make metadata remount for each render */}
947+
const metadata = <MetadataTree key={getFlightMetadataKey(requestId)} />
950948

951949
const initialHead = (
952950
<React.Fragment key={flightDataPathHeadKey}>
@@ -956,7 +954,7 @@ async function getErrorRSCPayload(
956954
isPossibleServerAction={ctx.isPossibleServerAction}
957955
/>
958956
{/* Adding requestId as react key to make metadata remount for each render */}
959-
<ViewportTree key={requestId} />
957+
<ViewportTree key={getFlightViewportKey(requestId)} />
960958
{process.env.NODE_ENV === 'development' && (
961959
<meta name="next-error" content="not-found" />
962960
)}

0 commit comments

Comments
 (0)