Skip to content

[Flight] Serialize rate limited objects in console logs as marker an increase limit #30294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -3158,10 +3162,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++;
Expand Down
Loading