Skip to content

Commit

Permalink
Move the Error creation to be lazy (#24728)
Browse files Browse the repository at this point in the history
Creating an Error captures a stack trace which can be somewhat expensive.
We shouldn't do tthat always for every render.

This also ensures that the stack trace is more useful because you can
follow through the Node.js code to see the cause.
  • Loading branch information
sebmarkbage authored Jun 15, 2022
1 parent 256aefb commit 522f473
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions packages/react-dom/src/server/ReactDOMFizzServerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function createDrainHandler(destination, request) {
}

function createAbortHandler(request, reason) {
return () => abort(request, reason);
// eslint-disable-next-line react-internal/prod-error-codes
return () => abort(request, new Error(reason));
}

type Options = {|
Expand All @@ -49,7 +50,7 @@ type Options = {|
type PipeableStream = {|
// Cancel any pending I/O and put anything remaining into
// client rendered mode.
abort(): void,
abort(reason: mixed): void,
pipe<T: Writable>(destination: T): T,
|};

Expand Down Expand Up @@ -94,21 +95,16 @@ function renderToPipeableStream(
'error',
createAbortHandler(
request,
// eslint-disable-next-line react-internal/prod-error-codes
new Error('The destination stream errored while writing data.'),
'The destination stream errored while writing data.',
),
);
destination.on(
'close',
createAbortHandler(
request,
// eslint-disable-next-line react-internal/prod-error-codes
new Error('The destination stream closed early.'),
),
createAbortHandler(request, 'The destination stream closed early.'),
);
return destination;
},
abort(reason) {
abort(reason: mixed) {
abort(request, reason);
},
};
Expand Down

0 comments on commit 522f473

Please sign in to comment.