Skip to content

Commit

Permalink
[Flight] Add option to replay console logs or not (#30207)
Browse files Browse the repository at this point in the history
Defaults to true in browser builds, otherwise defaults to false. The
assumption is that the server logs will already contain a log from the
original Flight server.

We currently always replay console logs but this leads to duplicates on
the server by default when you use SSR, because the Flight Client on the
server replays the logs. This can be nice since those logs gets badged.
It can also be nice if they're running in separate servers but when
they're logging to the same stream it's annoying. Which is really the
typical set up so we should just make that the default but leave it
configurable.
  • Loading branch information
sebmarkbage committed Jul 4, 2024
1 parent 3da2616 commit 8e9de89
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export type Response = {
_tempRefs: void | TemporaryReferenceSet, // the set temporary references can be resolved from
_debugRootTask?: null | ConsoleTask, // DEV-only
_debugFindSourceMapURL?: void | FindSourceMapURLCallback, // DEV-only
_replayConsole: boolean, // DEV-only
};

function readChunk<T>(chunk: SomeChunk<T>): T {
Expand Down Expand Up @@ -1278,6 +1279,7 @@ function ResponseInstance(
nonce: void | string,
temporaryReferences: void | TemporaryReferenceSet,
findSourceMapURL: void | FindSourceMapURLCallback,
replayConsole: boolean,
) {
const chunks: Map<number, SomeChunk<any>> = new Map();
this._bundlerConfig = bundlerConfig;
Expand All @@ -1304,6 +1306,7 @@ function ResponseInstance(
}
if (__DEV__) {
this._debugFindSourceMapURL = findSourceMapURL;
this._replayConsole = replayConsole;
}
// Don't inline this call because it causes closure to outline the call above.
this._fromJSON = createFromJSONCallback(this);
Expand All @@ -1317,6 +1320,7 @@ export function createResponse(
nonce: void | string,
temporaryReferences: void | TemporaryReferenceSet,
findSourceMapURL: void | FindSourceMapURLCallback,
replayConsole: boolean,
): Response {
// $FlowFixMe[invalid-constructor]: the shapes are exact here but Flow doesn't like constructors
return new ResponseInstance(
Expand All @@ -1327,6 +1331,7 @@ export function createResponse(
nonce,
temporaryReferences,
findSourceMapURL,
replayConsole,
);
}

Expand Down Expand Up @@ -2034,6 +2039,10 @@ function resolveConsoleEntry(
);
}

if (!response._replayConsole) {
return;
}

const payload: [string, string, null | ReactComponentInfo, string, mixed] =
parseModel(response, value);
const methodName = payload[0];
Expand Down
1 change: 1 addition & 0 deletions packages/react-html/src/ReactHTMLServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export function renderToMarkup(
undefined,
undefined,
undefined,
false,
);
const resumableState = createResumableState(
options ? options.identifierPrefix : undefined,
Expand Down
11 changes: 10 additions & 1 deletion packages/react-noop-renderer/src/ReactNoopFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ const {createResponse, processBinaryChunk, getRoot, close} = ReactFlightClient({
});

function read<T>(source: Source): Thenable<T> {
const response = createResponse(source, null);
const response = createResponse(
source,
null,
undefined,
undefined,
undefined,
undefined,
undefined,
true,
);
for (let i = 0; i < source.length; i++) {
processBinaryChunk(response, source[i], 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type Options = {
callServer?: CallServerCallback,
temporaryReferences?: TemporaryReferenceSet,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createResponseFromOptions(options: void | Options) {
Expand All @@ -57,6 +58,7 @@ function createResponseFromOptions(options: void | Options) {
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ ? (options ? options.replayConsoleLogs !== false : true) : false, // defaults to true
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/react-server-dom-esm/src/ReactFlightDOMClientNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type Options = {
nonce?: string,
encodeFormAction?: EncodeFormActionCallback,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createFromNodeStream<T>(
Expand All @@ -68,6 +69,7 @@ function createFromNodeStream<T>(
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
);
stream.on('data', chunk => {
processBinaryChunk(response, chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type Options = {
callServer?: CallServerCallback,
temporaryReferences?: TemporaryReferenceSet,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createResponseFromOptions(options: void | Options) {
Expand All @@ -56,6 +57,7 @@ function createResponseFromOptions(options: void | Options) {
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ ? (options ? options.replayConsoleLogs !== false : true) : false, // defaults to true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type Options = {
encodeFormAction?: EncodeFormActionCallback,
temporaryReferences?: TemporaryReferenceSet,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createResponseFromOptions(options: Options) {
Expand All @@ -86,6 +87,7 @@ function createResponseFromOptions(options: Options) {
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type Options = {
nonce?: string,
encodeFormAction?: EncodeFormActionCallback,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createFromNodeStream<T>(
Expand All @@ -77,6 +78,7 @@ function createFromNodeStream<T>(
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
);
stream.on('data', chunk => {
processBinaryChunk(response, chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type Options = {
callServer?: CallServerCallback,
temporaryReferences?: TemporaryReferenceSet,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createResponseFromOptions(options: void | Options) {
Expand All @@ -56,6 +57,7 @@ function createResponseFromOptions(options: void | Options) {
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ ? (options ? options.replayConsoleLogs !== false : true) : false, // defaults to true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type Options = {
encodeFormAction?: EncodeFormActionCallback,
temporaryReferences?: TemporaryReferenceSet,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createResponseFromOptions(options: Options) {
Expand All @@ -86,6 +87,7 @@ function createResponseFromOptions(options: Options) {
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type Options = {
nonce?: string,
encodeFormAction?: EncodeFormActionCallback,
findSourceMapURL?: FindSourceMapURLCallback,
replayConsoleLogs?: boolean,
};

function createFromNodeStream<T>(
Expand All @@ -78,6 +79,7 @@ function createFromNodeStream<T>(
__DEV__ && options && options.findSourceMapURL
? options.findSourceMapURL
: undefined,
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
);
stream.on('data', chunk => {
if (typeof chunk === 'string') {
Expand Down

0 comments on commit 8e9de89

Please sign in to comment.