Skip to content
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
1 change: 1 addition & 0 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3276,6 +3276,7 @@ describe('ReactFlight', () => {
expect(typeof loggedFn2).toBe('function');
expect(loggedFn2).not.toBe(foo);
expect(loggedFn2.toString()).toBe(foo.toString());
expect(loggedFn2).toBe(loggedFn);

const promise = mockConsoleLog.mock.calls[0][1].promise;
expect(promise).toBeInstanceOf(Promise);
Expand Down
21 changes: 19 additions & 2 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4096,8 +4096,25 @@ function renderConsoleValue(
}

// Serialize the body of the function as an eval so it can be printed.
// $FlowFixMe[method-unbinding]
return serializeEval('(' + Function.prototype.toString.call(value) + ')');
const writtenObjects = request.writtenObjects;
const existingReference = writtenObjects.get(value);
if (existingReference !== undefined) {
// We've already emitted this function, so we can
// just refer to that by its existing reference.
return existingReference;
}

const serializedValue = serializeEval(
// $FlowFixMe[method-unbinding]
'(' + Function.prototype.toString.call(value) + ')',
);
request.pendingChunks++;
const id = request.nextChunkId++;
const processedChunk = encodeReferenceChunk(request, id, serializedValue);
request.completedRegularChunks.push(processedChunk);
const reference = serializeByValueID(id);
writtenObjects.set(value, reference);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem with this set today that it's not safe to write into this from renderConsoleValue because renderConsoleValue has a different serialization logic (e.g. it can serialize functions like this and can cut off properties at a certain depth).

That's why we don't actually dedupe objects neither unless they also appear in the actual RSC payload elsewhere. Currently we only read from it and assume that if you're sending it across anyway, then whatever serialization happened in the production path is good enough also for the console value path.

We should really have a separate writtenObjects set just for the renderConsoleValue serialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that makes sense. Thanks for handling the follow up!

return reference;
}

if (typeof value === 'symbol') {
Expand Down
Loading