From 1ffb19e65c0852e58a485689675158c1f1ebfdb1 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 5 Nov 2020 19:45:21 -0500 Subject: [PATCH] Reuse symbol ids that have already been written earlier in the stream --- packages/react-server/src/ReactFlightServer.js | 8 ++++++++ 1 file changed, 8 insertions(+) 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); }