diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index ae90ef7e24cf9..b6afb5f2e824c 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -79,6 +79,7 @@ export type Request = { completedModuleChunks: Array, completedJSONChunks: Array, completedErrorChunks: Array, + writtenSymbols: Map, flowing: boolean, toJSON: (key: string, value: ReactModel) => ReactJSONValue, }; @@ -100,6 +101,7 @@ export function createRequest( completedModuleChunks: [], completedJSONChunks: [], completedErrorChunks: [], + writtenSymbols: new Map(), flowing: false, toJSON: function(key: string, value: ReactModel): ReactJSONValue { return resolveModelToJSON(request, this, key, value); @@ -528,9 +530,15 @@ export function resolveModelToJSON( describeKeyForErrorMessage(key), describeObjectForErrorMessage(parent), ); + const writtenSymbols = request.writtenSymbols; + const existingId = writtenSymbols.get(value); + if (existingId !== undefined) { + return serializeByValueID(existingId); + } request.pendingChunks++; const symbolId = request.nextChunkId++; emitSymbolChunk(request, symbolId, name); + writtenSymbols.set(value, symbolId); return serializeByValueID(symbolId); }