Skip to content

Commit

Permalink
[Flight] Instrument the Console in the RSC Environment and Replay Log…
Browse files Browse the repository at this point in the history
…s 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)
  • Loading branch information
sebmarkbage committed Feb 21, 2024
1 parent acca79d commit 238477b
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 21 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dc30644ca77e52a2760e81fbdbcfbd2f2fd4979c
9a5b6bd84ffa69bfd8b2859ce23e56d17daa8c40
6 changes: 3 additions & 3 deletions compiled/facebook-www/ReactDOMTesting-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -17067,7 +17067,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1783 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-modern-26b0c901",
version: "18.3.0-www-modern-90273ada",
rendererPackageName: "react-dom"
};
var internals$jscomp$inline_2154 = {
Expand Down Expand Up @@ -17098,7 +17098,7 @@ var internals$jscomp$inline_2154 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-26b0c901"
reconcilerVersion: "18.3.0-www-modern-90273ada"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_2155 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down Expand Up @@ -17519,4 +17519,4 @@ exports.useFormState = function (action, initialState, permalink) {
exports.useFormStatus = function () {
return ReactCurrentDispatcher$2.current.useHostTransitionStatus();
};
exports.version = "18.3.0-www-modern-26b0c901";
exports.version = "18.3.0-www-modern-90273ada";
38 changes: 38 additions & 0 deletions compiled/facebook-www/ReactFlightDOMClient-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ if (__DEV__) {

case "@": {
// Promise
if (value.length === 2) {
// Infinite promise that never resolves.
return new Promise(function () {});
}

var _id = parseInt(value.slice(2), 16);

var _chunk = getChunk(response, _id);
Expand Down Expand Up @@ -771,6 +776,21 @@ if (__DEV__) {
return BigInt(value.slice(2));
}

case "E": {
{
// In DEV mode we allow indirect eval to produce functions for logging.
// This should not compile to eval() because then it has local scope access.
try {
// eslint-disable-next-line no-eval
return (0, eval)(value.slice(2));
} catch (x) {
// We currently use this to express functions so we fail parsing it,
// let's just return a blank function as a place holder.
return function () {};
}
} // Fallthrough
}

default: {
// We assume that anything else is a reference ID.
var _id5 = parseInt(value.slice(1), 16);
Expand Down Expand Up @@ -987,6 +1007,16 @@ if (__DEV__) {
chunkDebugInfo.push(debugInfo);
}

function resolveConsoleEntry(response, value) {
var payload = parseModel(response, value);
var methodName = payload[0]; // TODO: Restore the fake stack before logging.
// const stackTrace = payload[1];

var args = payload.slice(2); // eslint-disable-next-line react-internal/no-production-logging

console[methodName].apply(console, args);
}

function processFullRow(response, id, tag, buffer, chunk) {
var stringDecoder = response._stringDecoder;
var row = "";
Expand Down Expand Up @@ -1040,6 +1070,14 @@ if (__DEV__) {
var debugInfo = JSON.parse(row);
resolveDebugInfo(response, id, debugInfo);
return;
} // Fallthrough to share the error with Console entries.
}

case 87: /* "W" */
{
{
resolveConsoleEntry(response, row);
return;
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiled/facebook-www/ReactFlightDOMClient-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ function parseModelString(response, parentObject, key, value) {
{ $$typeof: REACT_LAZY_TYPE, _payload: response, _init: readChunk }
);
case "@":
return (
(parentObject = parseInt(value.slice(2), 16)),
getChunk(response, parentObject)
);
if (2 === value.length) return new Promise(function () {});
parentObject = parseInt(value.slice(2), 16);
return getChunk(response, parentObject);
case "S":
return Symbol.for(value.slice(2));
case "F":
Expand Down Expand Up @@ -507,6 +506,7 @@ function startReadingFromStream(response, stream) {
);
break;
case 68:
case 87:
throw Error(
"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."
);
Expand Down
Loading

0 comments on commit 238477b

Please sign in to comment.