@@ -509,6 +509,7 @@ function createErrorContext(
509
509
async function generateDynamicFlightRenderResult (
510
510
req : BaseNextRequest ,
511
511
ctx : AppRenderContext ,
512
+ requestStore : RequestStore ,
512
513
options ?: {
513
514
actionResult : ActionResult
514
515
skipFlight : boolean
@@ -534,7 +535,12 @@ async function generateDynamicFlightRenderResult(
534
535
const RSCPayload : RSCPayload & {
535
536
/** Only available during dynamicIO development builds. Used for logging errors. */
536
537
_validation ?: Promise < React . ReactNode >
537
- } = await generateDynamicRSCPayload ( ctx , options )
538
+ } = await workUnitAsyncStorage . run (
539
+ requestStore ,
540
+ generateDynamicRSCPayload ,
541
+ ctx ,
542
+ options
543
+ )
538
544
539
545
if (
540
546
// We only want this behavior when running `next dev`
@@ -559,7 +565,9 @@ async function generateDynamicFlightRenderResult(
559
565
560
566
// For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
561
567
// which contains the subset React.
562
- const flightReadableStream = ctx . componentMod . renderToReadableStream (
568
+ const flightReadableStream = workUnitAsyncStorage . run (
569
+ requestStore ,
570
+ ctx . componentMod . renderToReadableStream ,
563
571
RSCPayload ,
564
572
ctx . clientReferenceManifest . clientModules ,
565
573
{
@@ -583,13 +591,7 @@ async function generateDynamicFlightRenderResult(
583
591
async function warmupDevRender (
584
592
req : BaseNextRequest ,
585
593
ctx : AppRenderContext ,
586
- requestStore : RequestStore ,
587
- options ?: {
588
- actionResult : ActionResult
589
- skipFlight : boolean
590
- componentTree ?: CacheNodeSeedData
591
- preloadCallbacks ?: PreloadCallbacks
592
- }
594
+ requestStore : RequestStore
593
595
) : Promise < RenderResult > {
594
596
const renderOpts = ctx . renderOpts
595
597
if ( ! renderOpts . dev ) {
@@ -622,8 +624,7 @@ async function warmupDevRender(
622
624
const rscPayload = await workUnitAsyncStorage . run (
623
625
requestStore ,
624
626
generateDynamicRSCPayload ,
625
- ctx ,
626
- options
627
+ ctx
627
628
)
628
629
629
630
// For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
@@ -989,12 +990,12 @@ async function renderToHTMLOrFlightImpl(
989
990
pagePath : string ,
990
991
query : NextParsedUrlQuery ,
991
992
renderOpts : RenderOpts ,
992
- requestStore : RequestStore ,
993
993
workStore : WorkStore ,
994
994
parsedRequestHeaders : ParsedRequestHeaders ,
995
995
requestEndedState : { ended ?: boolean } ,
996
996
postponedState : PostponedState | null ,
997
- implicitTags : Array < string >
997
+ implicitTags : Array < string > ,
998
+ serverComponentsHmrCache : ServerComponentsHmrCache | undefined
998
999
) {
999
1000
const isNotFoundPath = pagePath === '/404'
1000
1001
if ( isNotFoundPath ) {
@@ -1047,26 +1048,6 @@ async function renderToHTMLOrFlightImpl(
1047
1048
isNodeNextRequest ( req )
1048
1049
) {
1049
1050
req . originalRequest . on ( 'end' , ( ) => {
1050
- const prerenderStore = workUnitAsyncStorage . getStore ( )
1051
- const isPPR =
1052
- prerenderStore &&
1053
- ( prerenderStore . type === 'prerender' ||
1054
- prerenderStore . type === 'prerender-ppr' )
1055
- ? ! ! prerenderStore . dynamicTracking ?. dynamicAccesses ?. length
1056
- : false
1057
-
1058
- if (
1059
- process . env . NODE_ENV === 'development' &&
1060
- renderOpts . setAppIsrStatus &&
1061
- ! isPPR &&
1062
- ! requestStore . usedDynamic &&
1063
- ! workStore . forceDynamic
1064
- ) {
1065
- // only node can be ISR so we only need to update the status here
1066
- const { pathname } = new URL ( req . url || '/' , 'http://n' )
1067
- renderOpts . setAppIsrStatus ( pathname , true )
1068
- }
1069
-
1070
1051
requestEndedState . ended = true
1071
1052
1072
1053
if ( 'performance' in globalThis ) {
@@ -1130,6 +1111,7 @@ async function renderToHTMLOrFlightImpl(
1130
1111
isPrefetchRequest,
1131
1112
isRSCRequest,
1132
1113
isDevWarmupRequest,
1114
+ isHmrRefresh,
1133
1115
nonce,
1134
1116
} = parsedRequestHeaders
1135
1117
@@ -1287,10 +1269,44 @@ async function renderToHTMLOrFlightImpl(
1287
1269
return new RenderResult ( await streamToString ( response . stream ) , options )
1288
1270
} else {
1289
1271
// We're rendering dynamically
1272
+ const renderResumeDataCache =
1273
+ renderOpts . devWarmupRenderResumeDataCache ??
1274
+ postponedState ?. renderResumeDataCache
1275
+
1276
+ const requestStore = createRequestStoreForRender (
1277
+ req ,
1278
+ res ,
1279
+ url ,
1280
+ implicitTags ,
1281
+ renderOpts . onUpdateCookies ,
1282
+ renderOpts . previewProps ,
1283
+ isHmrRefresh ,
1284
+ serverComponentsHmrCache ,
1285
+ renderResumeDataCache
1286
+ )
1287
+
1288
+ if (
1289
+ process . env . NODE_ENV === 'development' &&
1290
+ renderOpts . setAppIsrStatus &&
1291
+ // The type check here ensures that `req` is correctly typed, and the
1292
+ // environment variable check provides dead code elimination.
1293
+ process . env . NEXT_RUNTIME !== 'edge' &&
1294
+ isNodeNextRequest ( req )
1295
+ ) {
1296
+ const setAppIsrStatus = renderOpts . setAppIsrStatus
1297
+ req . originalRequest . on ( 'end' , ( ) => {
1298
+ if ( ! requestStore . usedDynamic && ! workStore . forceDynamic ) {
1299
+ // only node can be ISR so we only need to update the status here
1300
+ const { pathname } = new URL ( req . url || '/' , 'http://n' )
1301
+ setAppIsrStatus ( pathname , true )
1302
+ }
1303
+ } )
1304
+ }
1305
+
1290
1306
if ( isDevWarmupRequest ) {
1291
1307
return warmupDevRender ( req , ctx , requestStore )
1292
1308
} else if ( isRSCRequest ) {
1293
- return generateDynamicFlightRenderResult ( req , ctx )
1309
+ return generateDynamicFlightRenderResult ( req , ctx , requestStore )
1294
1310
}
1295
1311
1296
1312
const renderToStreamWithTracing = getTracer ( ) . wrap (
@@ -1415,7 +1431,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
1415
1431
isRoutePPREnabled : renderOpts . experimental . isRoutePPREnabled === true ,
1416
1432
} )
1417
1433
1418
- const { isHmrRefresh , isPrefetchRequest } = parsedRequestHeaders
1434
+ const { isPrefetchRequest } = parsedRequestHeaders
1419
1435
1420
1436
const requestEndedState = { ended : false }
1421
1437
let postponedState : PostponedState | null = null
@@ -1444,32 +1460,12 @@ export const renderToHTMLOrFlight: AppPageRender = (
1444
1460
)
1445
1461
}
1446
1462
1447
- const renderResumeDataCache =
1448
- renderOpts . devWarmupRenderResumeDataCache ??
1449
- postponedState ?. renderResumeDataCache
1450
-
1451
1463
const implicitTags = getImplicitTags (
1452
1464
renderOpts . routeModule . definition . page ,
1453
1465
url ,
1454
1466
fallbackRouteParams
1455
1467
)
1456
1468
1457
- // TODO: We need to refactor this so that prerenders do not rely upon the
1458
- // existence of an outer scoped request store. Then we should move this
1459
- // store generation inside the appropriate scope like `renderToStream` where
1460
- // we know we're handling a Request and not a Prerender
1461
- const requestStore = createRequestStoreForRender (
1462
- req ,
1463
- res ,
1464
- url ,
1465
- implicitTags ,
1466
- renderOpts . onUpdateCookies ,
1467
- renderResumeDataCache ,
1468
- renderOpts . previewProps ,
1469
- isHmrRefresh ,
1470
- serverComponentsHmrCache
1471
- )
1472
-
1473
1469
const workStore = createWorkStore ( {
1474
1470
page : renderOpts . routeModule . definition . page ,
1475
1471
fallbackRouteParams,
@@ -1479,24 +1475,24 @@ export const renderToHTMLOrFlight: AppPageRender = (
1479
1475
isPrefetchRequest,
1480
1476
} )
1481
1477
1482
- return workAsyncStorage . run ( workStore , ( ) => {
1483
- return workUnitAsyncStorage . run ( requestStore , ( ) => {
1484
- return renderToHTMLOrFlightImpl (
1485
- req ,
1486
- res ,
1487
- url ,
1488
- pagePath ,
1489
- query ,
1490
- renderOpts ,
1491
- requestStore ,
1492
- workStore ,
1493
- parsedRequestHeaders ,
1494
- requestEndedState ,
1495
- postponedState ,
1496
- implicitTags
1497
- )
1498
- } )
1499
- } )
1478
+ return workAsyncStorage . run (
1479
+ workStore ,
1480
+ // The function to run
1481
+ renderToHTMLOrFlightImpl ,
1482
+ // all of it's args
1483
+ req ,
1484
+ res ,
1485
+ url ,
1486
+ pagePath ,
1487
+ query ,
1488
+ renderOpts ,
1489
+ workStore ,
1490
+ parsedRequestHeaders ,
1491
+ requestEndedState ,
1492
+ postponedState ,
1493
+ implicitTags ,
1494
+ serverComponentsHmrCache
1495
+ )
1500
1496
}
1501
1497
1502
1498
async function renderToStream (
0 commit comments