From 4dd03e3cef951989ca6cff0b5ea17039da54f086 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 16eefdc0080ed..1acaa8b3b79c8 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -78,6 +78,7 @@ export type Request = { completedModuleChunks: Array, completedJSONChunks: Array, completedErrorChunks: Array, + writtenSymbols: Map, flowing: boolean, toJSON: (key: string, value: ReactModel) => ReactJSONValue, }; @@ -99,6 +100,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); @@ -527,9 +529,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); }