diff --git a/packages/react-client/src/ReactFlightClient.js b/packages/react-client/src/ReactFlightClient.js index cc0361e6b8dc2..49da30ccf4c17 100644 --- a/packages/react-client/src/ReactFlightClient.js +++ b/packages/react-client/src/ReactFlightClient.js @@ -813,6 +813,12 @@ function createInitializedStreamChunk< value: T, controller: FlightStreamController, ): InitializedChunk { + if (__DEV__) { + // Retain a strong reference to the Response while we wait for chunks. + if (response._pendingChunks++ === 0) { + response._weakResponse.response = response; + } + } // We use the reason field to stash the controller since we already have that // field. It's a bit of a hack but efficient. // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors @@ -3075,7 +3081,6 @@ function resolveStream>( // We already resolved. We didn't expect to see this. return; } - releasePendingChunk(response, chunk); const resolveListeners = chunk.value; @@ -3375,6 +3380,14 @@ function stopStream( // We didn't expect not to have an existing stream; return; } + if (__DEV__) { + if (--response._pendingChunks === 0) { + // We're no longer waiting for any more chunks. We can release the strong + // reference to the response. We'll regain it if we ask for any more data + // later on. + response._weakResponse.response = null; + } + } const streamChunk: InitializedStreamChunk = (chunk: any); const controller = streamChunk.reason; controller.close(row === '' ? '"$undefined"' : row);