From 52ea641449570bbc32eb90fb1a76740249b6bcf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Mon, 5 May 2025 11:37:39 -0400 Subject: [PATCH] [Flight] Don't increase serializedSize for every recursive pass (#33123) I noticed that we increase this in the recursive part of the algorithm. This would mean that we'd count a key more than once if it has Server Components inside it recursively resolving. This moves it out to where we enter from toJSON. Which is called once per JSON entry (and therefore once per key). --- packages/react-server/src/ReactFlightServer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index aefcf5f6ee..0fcd8af7cb 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -2302,6 +2302,9 @@ function renderModel( key: string, value: ReactClientValue, ): ReactJSONValue { + // First time we're serializing the key, we should add it to the size. + serializedSize += key.length; + const prevKeyPath = task.keyPath; const prevImplicitSlot = task.implicitSlot; try { @@ -2416,8 +2419,6 @@ function renderModelDestructive( // Set the currently rendering model task.model = value; - serializedSize += parentPropertyName.length; - // Special Symbol, that's very common. if (value === REACT_ELEMENT_TYPE) { return '$';