Repository: facebook/react
File: packages/react-server/src/ReactFizzServer.js (JavaScript)

Code before:
  const row = boundary.row;
  if (
    boundary.status === PENDING &&
    row !== null &&
    row.together &&
    row.boundaries !== null &&
    row.pendingTasks === row.boundaries.length
  ) {
    // If we have a "together" row and all the pendingTasks are really the boundaries themselves,
    // and we won't outline any of them then we can unblock this row early so that we can inline
    // all the boundaries at once.
    let allComplete = true;
    for (let i = 0; i < row.boundaries.length; i++) {
      const rowBoundary = row.boundaries[i];
      if (
        rowBoundary.pendingTasks !== 1 ||
        isEligibleForOutlining(request, rowBoundary)
      ) {
        allComplete = false;
        break;
      }
    }
    if (allComplete) {
      unblockSuspenseListRow(request, row);
    }
  }

  boundary.parentFlushed = true;
  // This segment is a Suspense boundary. We need to decide whether to
  // emit the content or the fallback now.
  if (boundary.status === CLIENT_RENDERED) {
    // Emit a client rendered suspense boundary wrapper.
    // We never queue the inner boundary so we'll never emit its content or partial segments.

    if (row !== null) {
      // Since this boundary end up client rendered, we can unblock future suspense list rows.
      // This means that they may appear out of order if the future rows succeed but this is
      // a client rendered row.
      if (--row.pendingTasks === 0) {
        finishSuspenseListRow(request, row);
      }
    }

    if (__DEV__) {
      writeStartClientRenderedSuspenseBoundary(
        destination,
        request.renderState,
        boundary.errorDigest,
        boundary.errorMessage,
        boundary.errorStack,
        boundary.errorComponentStack,

Review comment (suggestion):
This optimization wouldn't be safe if this had some hoistable state because it would not have been hoisted as part of `hoistPreambleState` since these boundaries weren't `COMPLETE` by the time we started flushing the shell.

Not sure how to solve that.

Code after:
        // Write all the chunks up until the next child.
        for (; chunkIdx < nextChild.index; chunkIdx++) {
          writeChunk(destination, chunks[chunkIdx]);
        }
        r = flushSegment(request, destination, nextChild, hoistableState);
      }
      // Finally just write all the remaining chunks
      for (; chunkIdx < chunks.length - 1; chunkIdx++) {
        writeChunk(destination, chunks[chunkIdx]);
      }
      if (chunkIdx < chunks.length) {
        r = writeChunkAndReturn(destination, chunks[chunkIdx]);
      }
      return r;
    }
    default: {
      throw new Error(
        'Aborted, errored or already flushed boundaries should not be flushed again. This is a bug in React.',
      );
    }
  }
}

// Running count for how much bytes of boundaries have flushed inlined into the currently
// flushing root or completed boundary.
let flushedByteSize = 0;

function flushSegment(
  request: Request,
  destination: Destination,
  segment: Segment,
  hoistableState: null | HoistableState,
): boolean {
  const boundary = segment.boundary;
  if (boundary === null) {
    // Not a suspense boundary.
    return flushSubtree(request, destination, segment, hoistableState);
  }

  boundary.parentFlushed = true;
  // This segment is a Suspense boundary. We need to decide whether to
  // emit the content or the fallback now.
  if (boundary.status === CLIENT_RENDERED) {
    // Emit a client rendered suspense boundary wrapper.
    // We never queue the inner boundary so we'll never emit its content or partial segments.

    const row = boundary.row;
    if (row !== null) {
      // Since this boundary end up client rendered, we can unblock future suspense list rows.
      // This means that they may appear out of order if the future rows succeed but this is
      // a client rendered row.