MCPcopy
hub / github.com/facebook/react / preparePreambleFromSegment

Function preparePreambleFromSegment

packages/react-server/src/ReactFizzServer.js:5493–5565  ·  view source on GitHub ↗
(
  request: Request,
  segment: Segment,
  collectedPreambleSegments: Array<Array<Segment>>,
)

Source from the content-addressed store, hash-verified

5491}
5492
5493function preparePreambleFromSegment(
5494 request: Request,
5495 segment: Segment,
5496 collectedPreambleSegments: Array<Array<Segment>>,
5497): boolean {
5498 const boundary = segment.boundary;
5499 if (boundary === null) {
5500 // This segment is not a boundary, let's check it's children
5501 return preparePreambleFromSubtree(
5502 request,
5503 segment,
5504 collectedPreambleSegments,
5505 );
5506 }
5507
5508 const preamble = boundary.contentPreamble;
5509 const fallbackPreamble = boundary.fallbackPreamble;
5510
5511 if (preamble === null || fallbackPreamble === null) {
5512 // This boundary cannot have a preamble so it can't block the flushing of
5513 // the preamble.
5514 return false;
5515 }
5516
5517 const status = boundary.status;
5518
5519 switch (status) {
5520 case COMPLETED: {
5521 // This boundary is complete. It might have inner boundaries which are pending
5522 // and able to provide a preamble so we have to check it's children
5523 hoistPreambleState(request.renderState, preamble);
5524 // We track this boundary's byteSize on the request since it will always flush with
5525 // the request since it may contribute to the preamble
5526 request.byteSize += boundary.byteSize;
5527 const boundaryRootSegment = boundary.completedSegments[0];
5528 if (!boundaryRootSegment) {
5529 // Using the same error from flushSegment to avoid making a new one since conceptually the problem is still the same
5530 throw new Error(
5531 'A previously unvisited boundary must have exactly one root segment. This is a bug in React.',
5532 );
5533 }
5534 return preparePreambleFromSubtree(
5535 request,
5536 boundaryRootSegment,
5537 collectedPreambleSegments,
5538 );
5539 }
5540 case POSTPONED: {
5541 // This segment is postponed. When prerendering we consider this pending still because
5542 // it can resume. If we're rendering then this is equivalent to errored.
5543 if (request.trackedPostpones !== null) {
5544 // This boundary won't contribute a preamble to the current prerender
5545 return true;
5546 }
5547 // Expected fallthrough
5548 }
5549 case CLIENT_RENDERED: {
5550 if (segment.status === COMPLETED) {

Callers 2

preparePreambleFunction · 0.85

Calls 2

hoistPreambleStateFunction · 0.90

Tested by

no test coverage detected