Skip to content

Commit 238477b

Browse files
committed
[Flight] Instrument the Console in the RSC Environment and Replay Logs on the Client (#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. DiffTrain build for [9a5b6bd](9a5b6bd)
1 parent acca79d commit 238477b

9 files changed

+391
-21
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
dc30644ca77e52a2760e81fbdbcfbd2f2fd4979c
1+
9a5b6bd84ffa69bfd8b2859ce23e56d17daa8c40

compiled/facebook-www/ReactDOMTesting-prod.modern.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17067,7 +17067,7 @@ Internals.Events = [
1706717067
var devToolsConfig$jscomp$inline_1783 = {
1706817068
findFiberByHostInstance: getClosestInstanceFromNode,
1706917069
bundleType: 0,
17070-
version: "18.3.0-www-modern-26b0c901",
17070+
version: "18.3.0-www-modern-90273ada",
1707117071
rendererPackageName: "react-dom"
1707217072
};
1707317073
var internals$jscomp$inline_2154 = {
@@ -17098,7 +17098,7 @@ var internals$jscomp$inline_2154 = {
1709817098
scheduleRoot: null,
1709917099
setRefreshHandler: null,
1710017100
getCurrentFiber: null,
17101-
reconcilerVersion: "18.3.0-www-modern-26b0c901"
17101+
reconcilerVersion: "18.3.0-www-modern-90273ada"
1710217102
};
1710317103
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1710417104
var hook$jscomp$inline_2155 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -17519,4 +17519,4 @@ exports.useFormState = function (action, initialState, permalink) {
1751917519
exports.useFormStatus = function () {
1752017520
return ReactCurrentDispatcher$2.current.useHostTransitionStatus();
1752117521
};
17522-
exports.version = "18.3.0-www-modern-26b0c901";
17522+
exports.version = "18.3.0-www-modern-90273ada";

compiled/facebook-www/ReactFlightDOMClient-dev.modern.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ if (__DEV__) {
699699

700700
case "@": {
701701
// Promise
702+
if (value.length === 2) {
703+
// Infinite promise that never resolves.
704+
return new Promise(function () {});
705+
}
706+
702707
var _id = parseInt(value.slice(2), 16);
703708

704709
var _chunk = getChunk(response, _id);
@@ -771,6 +776,21 @@ if (__DEV__) {
771776
return BigInt(value.slice(2));
772777
}
773778

779+
case "E": {
780+
{
781+
// In DEV mode we allow indirect eval to produce functions for logging.
782+
// This should not compile to eval() because then it has local scope access.
783+
try {
784+
// eslint-disable-next-line no-eval
785+
return (0, eval)(value.slice(2));
786+
} catch (x) {
787+
// We currently use this to express functions so we fail parsing it,
788+
// let's just return a blank function as a place holder.
789+
return function () {};
790+
}
791+
} // Fallthrough
792+
}
793+
774794
default: {
775795
// We assume that anything else is a reference ID.
776796
var _id5 = parseInt(value.slice(1), 16);
@@ -987,6 +1007,16 @@ if (__DEV__) {
9871007
chunkDebugInfo.push(debugInfo);
9881008
}
9891009

1010+
function resolveConsoleEntry(response, value) {
1011+
var payload = parseModel(response, value);
1012+
var methodName = payload[0]; // TODO: Restore the fake stack before logging.
1013+
// const stackTrace = payload[1];
1014+
1015+
var args = payload.slice(2); // eslint-disable-next-line react-internal/no-production-logging
1016+
1017+
console[methodName].apply(console, args);
1018+
}
1019+
9901020
function processFullRow(response, id, tag, buffer, chunk) {
9911021
var stringDecoder = response._stringDecoder;
9921022
var row = "";
@@ -1040,6 +1070,14 @@ if (__DEV__) {
10401070
var debugInfo = JSON.parse(row);
10411071
resolveDebugInfo(response, id, debugInfo);
10421072
return;
1073+
} // Fallthrough to share the error with Console entries.
1074+
}
1075+
1076+
case 87: /* "W" */
1077+
{
1078+
{
1079+
resolveConsoleEntry(response, row);
1080+
return;
10431081
}
10441082
}
10451083

compiled/facebook-www/ReactFlightDOMClient-prod.modern.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,9 @@ function parseModelString(response, parentObject, key, value) {
257257
{ $$typeof: REACT_LAZY_TYPE, _payload: response, _init: readChunk }
258258
);
259259
case "@":
260-
return (
261-
(parentObject = parseInt(value.slice(2), 16)),
262-
getChunk(response, parentObject)
263-
);
260+
if (2 === value.length) return new Promise(function () {});
261+
parentObject = parseInt(value.slice(2), 16);
262+
return getChunk(response, parentObject);
264263
case "S":
265264
return Symbol.for(value.slice(2));
266265
case "F":
@@ -507,6 +506,7 @@ function startReadingFromStream(response, stream) {
507506
);
508507
break;
509508
case 68:
509+
case 87:
510510
throw Error(
511511
"Failed to read a RSC payload created by a development version of React on the server while using a production version on the client. Always use matching versions on the server and the client."
512512
);

0 commit comments

Comments
 (0)