@@ -181,6 +181,7 @@ import {
181181 enableViewTransition ,
182182 enableFizzBlockingRender ,
183183 enableAsyncDebugInfo ,
184+ enableCPUSuspense ,
184185} from 'shared/ReactFeatureFlags' ;
185186
186187import assign from 'shared/assign' ;
@@ -250,6 +251,7 @@ type SuspenseBoundary = {
250251 row : null | SuspenseListRow , // the row that this boundary blocks from completing.
251252 completedSegments : Array < Segment > , // completed but not yet flushed segments.
252253 byteSize : number , // used to determine whether to inline children boundaries.
254+ defer : boolean , // never inline deferred boundaries
253255 fallbackAbortableTasks : Set < Task > , // used to cancel task on the fallback if the boundary completes or gets canceled.
254256 contentState : HoistableState ,
255257 fallbackState : HoistableState ,
@@ -456,7 +458,9 @@ function isEligibleForOutlining(
456458 // The larger this limit is, the more we can save on preparing fallbacks in case we end up
457459 // outlining.
458460 return (
459- ( boundary . byteSize > 500 || hasSuspenseyContent ( boundary . contentState ) ) &&
461+ ( boundary . byteSize > 500 ||
462+ hasSuspenseyContent ( boundary . contentState ) ||
463+ boundary . defer ) &&
460464 // For boundaries that can possibly contribute to the preamble we don't want to outline
461465 // them regardless of their size since the fallbacks should only be emitted if we've
462466 // errored the boundary.
@@ -782,6 +786,7 @@ function createSuspenseBoundary(
782786 fallbackAbortableTasks : Set < Task > ,
783787 contentPreamble: null | Preamble,
784788 fallbackPreamble: null | Preamble,
789+ defer: boolean,
785790): SuspenseBoundary {
786791 const boundary : SuspenseBoundary = {
787792 status : PENDING ,
@@ -791,6 +796,7 @@ function createSuspenseBoundary(
791796 row : row ,
792797 completedSegments : [ ] ,
793798 byteSize : 0 ,
799+ defer : defer ,
794800 fallbackAbortableTasks,
795801 errorDigest : null ,
796802 contentState : createHoistableState ( ) ,
@@ -1274,6 +1280,7 @@ function renderSuspenseBoundary(
12741280 // in case it ends up generating a large subtree of content.
12751281 const fallback : ReactNodeList = props . fallback ;
12761282 const content : ReactNodeList = props . children ;
1283+ const defer : boolean = enableCPUSuspense && props . defer === true ;
12771284
12781285 const fallbackAbortSet : Set < Task > = new Set ( ) ;
12791286 let newBoundary : SuspenseBoundary ;
@@ -1284,6 +1291,7 @@ function renderSuspenseBoundary(
12841291 fallbackAbortSet ,
12851292 createPreambleState ( ) ,
12861293 createPreambleState ( ) ,
1294+ defer ,
12871295 ) ;
12881296 } else {
12891297 newBoundary = createSuspenseBoundary (
@@ -1292,6 +1300,7 @@ function renderSuspenseBoundary(
12921300 fallbackAbortSet ,
12931301 null ,
12941302 null ,
1303+ defer ,
12951304 ) ;
12961305 }
12971306 if ( request . trackedPostpones !== null ) {
@@ -1327,29 +1336,32 @@ function renderSuspenseBoundary(
13271336 // no parent segment so there's nothing to wait on.
13281337 contentRootSegment . parentFlushed = true ;
13291338
1330- if ( request . trackedPostpones !== null ) {
1339+ const trackedPostpones = request . trackedPostpones ;
1340+ if ( trackedPostpones !== null || defer ) {
1341+ // This is a prerender or deferred boundary. In this mode we want to render the fallback synchronously
1342+ // and schedule the content to render later. This is the opposite of what we do during a normal render
1343+ // where we try to skip rendering the fallback if the content itself can render synchronously
1344+
13311345 // Stash the original stack frame.
13321346 const suspenseComponentStack = task . componentStack ;
1333- // This is a prerender. In this mode we want to render the fallback synchronously and schedule
1334- // the content to render later. This is the opposite of what we do during a normal render
1335- // where we try to skip rendering the fallback if the content itself can render synchronously
1336- const trackedPostpones = request . trackedPostpones ;
13371347
13381348 const fallbackKeyPath : KeyNode = [
13391349 keyPath [ 0 ] ,
13401350 'Suspense Fallback' ,
13411351 keyPath [ 2 ] ,
13421352 ] ;
1343- const fallbackReplayNode : ReplayNode = [
1344- fallbackKeyPath [ 1 ] ,
1345- fallbackKeyPath [ 2 ] ,
1346- ( [ ] : Array < ReplayNode > ) ,
1347- null ,
1348- ] ;
1349- trackedPostpones . workingMap . set ( fallbackKeyPath , fallbackReplayNode ) ;
1350- // We are rendering the fallback before the boundary content so we keep track of
1351- // the fallback replay node until we determine if the primary content suspends
1352- newBoundary . trackedFallbackNode = fallbackReplayNode ;
1353+ if ( trackedPostpones !== null ) {
1354+ const fallbackReplayNode : ReplayNode = [
1355+ fallbackKeyPath [ 1 ] ,
1356+ fallbackKeyPath [ 2 ] ,
1357+ ( [ ] : Array < ReplayNode > ) ,
1358+ null ,
1359+ ] ;
1360+ trackedPostpones . workingMap . set ( fallbackKeyPath , fallbackReplayNode ) ;
1361+ // We are rendering the fallback before the boundary content so we keep track of
1362+ // the fallback replay node until we determine if the primary content suspends
1363+ newBoundary . trackedFallbackNode = fallbackReplayNode ;
1364+ }
13531365
13541366 task . blockedSegment = boundarySegment ;
13551367 task . blockedPreamble = newBoundary . fallbackPreamble ;
@@ -1580,6 +1592,7 @@ function replaySuspenseBoundary(
15801592
15811593 const content : ReactNodeList = props . children ;
15821594 const fallback : ReactNodeList = props . fallback ;
1595+ const defer : boolean = enableCPUSuspense && props . defer === true ;
15831596
15841597 const fallbackAbortSet : Set < Task > = new Set ( ) ;
15851598 let resumedBoundary : SuspenseBoundary ;
@@ -1590,6 +1603,7 @@ function replaySuspenseBoundary(
15901603 fallbackAbortSet ,
15911604 createPreambleState ( ) ,
15921605 createPreambleState ( ) ,
1606+ defer ,
15931607 ) ;
15941608 } else {
15951609 resumedBoundary = createSuspenseBoundary (
@@ -1598,6 +1612,7 @@ function replaySuspenseBoundary(
15981612 fallbackAbortSet ,
15991613 null ,
16001614 null ,
1615+ defer ,
16011616 ) ;
16021617 }
16031618 resumedBoundary . parentFlushed = true ;
@@ -4384,6 +4399,7 @@ function abortRemainingSuspenseBoundary(
43844399 new Set ( ) ,
43854400 null ,
43864401 null ,
4402+ false ,
43874403 ) ;
43884404 resumedBoundary . parentFlushed = true ;
43894405 // We restore the same id of this boundary as was used during prerender.
@@ -5493,7 +5509,8 @@ function flushSegment(
54935509 ! flushingPartialBoundaries &&
54945510 isEligibleForOutlining ( request , boundary ) &&
54955511 ( flushedByteSize + boundary . byteSize > request . progressiveChunkSize ||
5496- hasSuspenseyContent ( boundary . contentState ) )
5512+ hasSuspenseyContent ( boundary . contentState ) ||
5513+ boundary . defer )
54975514 ) {
54985515 // Inlining this boundary would make the current sequence being written too large
54995516 // and block the parent for too long. Instead, it will be emitted separately so that we
0 commit comments