@@ -467,6 +467,16 @@ async function generateDynamicRSCPayload(
467
467
MetadataBoundary,
468
468
ViewportBoundary,
469
469
} )
470
+
471
+ const MetadataComponent = ( ) => {
472
+ return (
473
+ < React . Fragment key = { flightDataPathMetadataKey } >
474
+ { /* Adding requestId as react key to make metadata remount for each render */ }
475
+ < MetadataTree key = { requestId } />
476
+ </ React . Fragment >
477
+ )
478
+ }
479
+
470
480
flightData = (
471
481
await walkTreeWithFlightRouterState ( {
472
482
ctx,
@@ -481,10 +491,7 @@ async function generateDynamicRSCPayload(
481
491
{ /* Adding requestId as react key to make metadata remount for each render */ }
482
492
< ViewportTree key = { requestId } />
483
493
</ React . Fragment > ,
484
- < React . Fragment key = { flightDataPathMetadataKey } >
485
- { /* Adding requestId as react key to make metadata remount for each render */ }
486
- < MetadataTree key = { requestId } />
487
- </ React . Fragment > ,
494
+ null ,
488
495
] ,
489
496
injectedCSS : new Set ( ) ,
490
497
injectedJS : new Set ( ) ,
@@ -493,6 +500,7 @@ async function generateDynamicRSCPayload(
493
500
getViewportReady,
494
501
getMetadataReady,
495
502
preloadCallbacks,
503
+ MetadataComponent,
496
504
} )
497
505
) . map ( ( path ) => path . slice ( 1 ) ) // remove the '' (root) segment
498
506
}
@@ -766,6 +774,15 @@ async function getRSCPayload(
766
774
767
775
const preloadCallbacks : PreloadCallbacks = [ ]
768
776
777
+ function MetadataComponent ( ) {
778
+ return (
779
+ < React . Fragment key = { flightDataPathMetadataKey } >
780
+ { /* Not add requestId as react key to ensure segment prefetch could result consistently if nothing changed */ }
781
+ < MetadataTree />
782
+ </ React . Fragment >
783
+ )
784
+ }
785
+
769
786
const seedData = await createComponentTree ( {
770
787
ctx,
771
788
loaderTree : tree ,
@@ -779,6 +796,7 @@ async function getRSCPayload(
779
796
missingSlots,
780
797
preloadCallbacks,
781
798
authInterrupts : ctx . renderOpts . experimental . authInterrupts ,
799
+ MetadataComponent,
782
800
} )
783
801
784
802
// When the `vary` response header is present with `Next-URL`, that means there's a chance
@@ -788,13 +806,6 @@ async function getRSCPayload(
788
806
const couldBeIntercepted =
789
807
typeof varyHeader === 'string' && varyHeader . includes ( NEXT_URL )
790
808
791
- const initialHeadMetadata = (
792
- < React . Fragment key = { flightDataPathMetadataKey } >
793
- { /* Adding requestId as react key to make metadata remount for each render */ }
794
- < MetadataTree key = { ctx . requestId } />
795
- </ React . Fragment >
796
- )
797
-
798
809
const initialHeadViewport = (
799
810
< React . Fragment key = { flightDataPathViewportKey } >
800
811
< NonIndex ctx = { ctx } />
@@ -825,7 +836,7 @@ async function getRSCPayload(
825
836
[
826
837
initialTree ,
827
838
seedData ,
828
- [ initialHeadViewport , initialHeadMetadata ] ,
839
+ [ initialHeadViewport , null ] ,
829
840
isPossiblyPartialHead ,
830
841
] as FlightDataPath ,
831
842
] ,
@@ -892,6 +903,7 @@ async function getErrorRSCPayload(
892
903
< MetadataTree key = { requestId } />
893
904
</ React . Fragment >
894
905
)
906
+
895
907
const initialHeadViewport = (
896
908
< React . Fragment key = { flightDataPathViewportKey } >
897
909
< NonIndex ctx = { ctx } />
@@ -911,11 +923,11 @@ async function getErrorRSCPayload(
911
923
912
924
// For metadata notFound error there's no global not found boundary on top
913
925
// so we create a not found page with AppRouter
914
- const initialSeedData : CacheNodeSeedData = [
926
+ const seedData : CacheNodeSeedData = [
915
927
initialTree [ 0 ] ,
916
928
< html id = "__next_error__" >
917
- < head > </ head >
918
- < body > </ body >
929
+ < head > { initialHeadMetadata } </ head >
930
+ < body / >
919
931
</ html > ,
920
932
{ } ,
921
933
null ,
@@ -937,8 +949,8 @@ async function getErrorRSCPayload(
937
949
f : [
938
950
[
939
951
initialTree ,
940
- initialSeedData ,
941
- [ initialHeadViewport , initialHeadMetadata ] ,
952
+ seedData ,
953
+ [ initialHeadViewport , null ] ,
942
954
isPossiblyPartialHead ,
943
955
] as FlightDataPath ,
944
956
] ,
0 commit comments