Skip to content

Commit 19c3266

Browse files
sebmarkbageAndyPengc12
authored andcommitted
[Flight] Instrument the Console in the RSC Environment and Replay Logs on the Client (facebook#28384)
When developing in an RSC environment, you should be able to work in a single environment as if it was a unified environment. With thrown errors we already serialize them and then rethrow them on the client. Since by default we log them via onError both in Flight and Fizz, you can get the same log in the RSC runtime, the SSR runtime and on the client. With console logs made in SSR renders, you typically replay the same code during hydration on the client. So for example warnings already show up both in the SSR logs and on the client (although not guaranteed to be the same). You could just spend your time in the client and you'd be fine. Previously, RSC logs would not be replayed because they don't hydrate. So it's easy to miss warnings for example. With this approach, we replay RSC logs both during SSR so they end up in the SSR logs and on the client. That way you can just stay in the browser window during normal development cycles. You shouldn't have to care if your component is a server or client component when working on logical things or iterating on a product. With this change, you probably should mostly ignore the Flight log stream and just look at the client or maybe the SSR one. Unless you're digging into something specific. In particular if you just naively run both Flight and Fizz in the same terminal you get duplicates. I like to run out fixtures `yarn dev:region` and `yarn dev:global` in two separate terminals. Console logs may contain complex objects which can be inspected. Ideally a DevTools inspector could reach into the RSC server and remotely inspect objects using the remote inspection protocol. That way complex objects can be loaded on demand as you expand into them. However, that is a complex environment to set up and the server might not even be alive anymore by the time you inspect the objects. Therefore, I do a best effort to serialize the objects using the RSC protocol but limit the depth that can be rendered. This feature is only own in dev mode since it can be expensive. In a follow up, I'll give the logs a special styling treatment to clearly differentiate them from logs coming from the client. As well as deal with stacks.
1 parent 2e05076 commit 19c3266

30 files changed

+567
-33
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ function parseModelString(
670670
}
671671
case '@': {
672672
// Promise
673+
if (value.length === 2) {
674+
// Infinite promise that never resolves.
675+
return new Promise(() => {});
676+
}
673677
const id = parseInt(value.slice(2), 16);
674678
const chunk = getChunk(response, id);
675679
return chunk;
@@ -725,6 +729,21 @@ function parseModelString(
725729
// BigInt
726730
return BigInt(value.slice(2));
727731
}
732+
case 'E': {
733+
if (__DEV__) {
734+
// In DEV mode we allow indirect eval to produce functions for logging.
735+
// This should not compile to eval() because then it has local scope access.
736+
try {
737+
// eslint-disable-next-line no-eval
738+
return (0, eval)(value.slice(2));
739+
} catch (x) {
740+
// We currently use this to express functions so we fail parsing it,
741+
// let's just return a blank function as a place holder.
742+
return function () {};
743+
}
744+
}
745+
// Fallthrough
746+
}
728747
default: {
729748
// We assume that anything else is a reference ID.
730749
const id = parseInt(value.slice(1), 16);
@@ -1063,6 +1082,27 @@ function resolveDebugInfo(
10631082
chunkDebugInfo.push(debugInfo);
10641083
}
10651084

1085+
function resolveConsoleEntry(
1086+
response: Response,
1087+
value: UninitializedModel,
1088+
): void {
1089+
if (!__DEV__) {
1090+
// These errors should never make it into a build so we don't need to encode them in codes.json
1091+
// eslint-disable-next-line react-internal/prod-error-codes
1092+
throw new Error(
1093+
'resolveConsoleEntry should never be called in production mode. This is a bug in React.',
1094+
);
1095+
}
1096+
1097+
const payload: [string, string, mixed] = parseModel(response, value);
1098+
const methodName = payload[0];
1099+
// TODO: Restore the fake stack before logging.
1100+
// const stackTrace = payload[1];
1101+
const args = payload.slice(2);
1102+
// eslint-disable-next-line react-internal/no-production-logging
1103+
console[methodName].apply(console, args);
1104+
}
1105+
10661106
function mergeBuffer(
10671107
buffer: Array<Uint8Array>,
10681108
lastChunk: Uint8Array,
@@ -1212,6 +1252,13 @@ function processFullRow(
12121252
resolveDebugInfo(response, id, debugInfo);
12131253
return;
12141254
}
1255+
// Fallthrough to share the error with Console entries.
1256+
}
1257+
case 87 /* "W" */: {
1258+
if (__DEV__) {
1259+
resolveConsoleEntry(response, row);
1260+
return;
1261+
}
12151262
throw new Error(
12161263
'Failed to read a RSC payload created by a development version of React ' +
12171264
'on the server while using a production version on the client. Always use ' +

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,4 +1995,45 @@ describe('ReactFlight', () => {
19951995
</div>,
19961996
);
19971997
});
1998+
1999+
// @gate enableServerComponentLogs && __DEV__
2000+
it('replays logs, but not onError logs', async () => {
2001+
function foo() {
2002+
return 'hello';
2003+
}
2004+
function ServerComponent() {
2005+
console.log('hi', {prop: 123, fn: foo});
2006+
throw new Error('err');
2007+
}
2008+
2009+
let transport;
2010+
expect(() => {
2011+
// Reset the modules so that we get a new overridden console on top of the
2012+
// one installed by expect. This ensures that we still emit console.error
2013+
// calls.
2014+
jest.resetModules();
2015+
jest.mock('react', () => require('react/react.react-server'));
2016+
ReactServer = require('react');
2017+
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
2018+
transport = ReactNoopFlightServer.render({root: <ServerComponent />});
2019+
}).toErrorDev('err');
2020+
2021+
const log = console.log;
2022+
try {
2023+
console.log = jest.fn();
2024+
// The error should not actually get logged because we're not awaiting the root
2025+
// so it's not thrown but the server log also shouldn't be replayed.
2026+
await ReactNoopFlightClient.read(transport);
2027+
2028+
expect(console.log).toHaveBeenCalledTimes(1);
2029+
expect(console.log.mock.calls[0][0]).toBe('hi');
2030+
expect(console.log.mock.calls[0][1].prop).toBe(123);
2031+
const loggedFn = console.log.mock.calls[0][1].fn;
2032+
expect(typeof loggedFn).toBe('function');
2033+
expect(loggedFn).not.toBe(foo);
2034+
expect(loggedFn.toString()).toBe(foo.toString());
2035+
} finally {
2036+
console.log = log;
2037+
}
2038+
});
19982039
});

0 commit comments

Comments
 (0)