From 14fdd0e21c420deb4bb96fc1e9021b531543d15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 10 Jul 2024 00:15:15 -0400 Subject: [PATCH] [Flight] Serialize rate limited objects in console logs as marker an increase limit (#30294) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This marker can then be emitted as a getter. When this object gets accessed we use a special error to let the user know what is going on. Screenshot 2024-07-08 at 10 13 46 PM When you click the `...`: Screenshot 2024-07-08 at 10 13 56 PM I also increased the object limit in console logs. It was arbitrarily set very low before. These limits are per message. So if you have a loop of many logs it can quickly add up a lot of strain on the server memory and the client. This is trying to find some tradeoff. Unfortunately we don't really do much deduping in these logs so with cyclic objects it ends up maximizing the limit and then siblings aren't logged. Ideally we should be able to lazy load them but that requires a lot of plumbing to wire up so if we can avoid it we should try to. If we want to that though one idea is to use the getter to do a sync XHR to load more data but the server needs to retain the objects in memory for an unknown amount of time. The client could maybe send a signal to retain them until a weakref clean up but even then it kind of needs a heartbeat to let the server know the client is still alive. That's a lot of complexity. There's probably more we can do to optimize deduping and other parts of the protocol to make it possible to have even higher limits. --- .../react-client/src/ReactFlightClient.js | 23 +++++++++++++++++++ .../react-server/src/ReactFlightServer.js | 10 +++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/react-client/src/ReactFlightClient.js b/packages/react-client/src/ReactFlightClient.js index da835412f1e94..8243e67c51884 100644 --- a/packages/react-client/src/ReactFlightClient.js +++ b/packages/react-client/src/ReactFlightClient.js @@ -1231,6 +1231,29 @@ function parseModelString( } // Fallthrough } + case 'Y': { + if (__DEV__) { + // In DEV mode we encode omitted objects in logs as a getter that throws + // so that when you try to access it on the client, you know why that + // happened. + Object.defineProperty(parentObject, key, { + get: function () { + // We intentionally don't throw an error object here because it looks better + // without the stack in the console which isn't useful anyway. + // eslint-disable-next-line no-throw-literal + throw ( + 'This object has been omitted by React in the console log ' + + 'to avoid sending too much data from the server. Try logging smaller ' + + 'or more specific objects.' + ); + }, + enumerable: true, + configurable: false, + }); + return null; + } + // Fallthrough + } default: { // We assume that anything else is a reference ID. const ref = value.slice(1); diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index 491757f32c4cb..a0d4b9e140566 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -1851,6 +1851,10 @@ function serializeSymbolReference(name: string): string { return '$S' + name; } +function serializeLimitedObject(): string { + return '$Y'; +} + function serializeNumber(number: number): string | number { if (Number.isFinite(number)) { if (number === 0 && 1 / number === -Infinity) { @@ -3181,10 +3185,10 @@ function renderConsoleValue( } } - if (counter.objectCount > 20) { + if (counter.objectCount > 500) { // We've reached our max number of objects to serialize across the wire so we serialize this - // object but no properties inside of it, as a place holder. - return Array.isArray(value) ? [] : {}; + // as a marker so that the client can error when this is accessed by the console. + return serializeLimitedObject(); } counter.objectCount++;