@@ -2716,11 +2716,12 @@ async function prerenderToStream(
27162716 ctx ,
27172717 res . statusCode === 404
27182718 )
2719+ let prerenderIsPending = true
27192720 const reactServerResult = ( reactServerPrerenderResult =
27202721 await createReactServerPrerenderResult (
27212722 prerenderAndAbortInSequentialTasks (
2722- ( ) =>
2723- workUnitAsyncStorage . run (
2723+ async ( ) => {
2724+ const prerenderResult = await workUnitAsyncStorage . run (
27242725 // The store to scope
27252726 finalRenderPrerenderStore ,
27262727 // The function to run
@@ -2730,17 +2731,33 @@ async function prerenderToStream(
27302731 clientReferenceManifest . clientModules ,
27312732 {
27322733 onError : ( err : unknown ) => {
2734+ // TODO we can remove this once https://github.com/facebook/react/pull/31715 lands
2735+ // because we won't have onError calls when halting the prerender
27332736 if ( finalServerController . signal . aborted ) {
2734- serverIsDynamic = true
27352737 return
27362738 }
27372739
27382740 return serverComponentsErrorHandler ( err )
27392741 } ,
27402742 signal : finalServerController . signal ,
27412743 }
2742- ) ,
2744+ )
2745+ prerenderIsPending = false
2746+ return prerenderResult
2747+ } ,
27432748 ( ) => {
2749+ if ( finalServerController . signal . aborted ) {
2750+ // If the server controller is already aborted we must have called something
2751+ // that required aborting the prerender synchronously such as with new Date()
2752+ serverIsDynamic = true
2753+ return
2754+ }
2755+
2756+ if ( prerenderIsPending ) {
2757+ // If prerenderIsPending then we have blocked for longer than a Task and we assume
2758+ // there is something unfinished.
2759+ serverIsDynamic = true
2760+ }
27442761 finalServerController . abort ( )
27452762 }
27462763 )
0 commit comments