Skip to content

Commit

Permalink
Deprecate renderToStaticNodeStream (facebook#28872)
Browse files Browse the repository at this point in the history
This commit adds warnings indicating that `renderToStaticNodeStream`
will be removed in an upcoming React release. This API has been legacy,
is not widely used (renderToStaticMarkup is more common) and has
semantically eqiuvalent implementations with renderToReadableStream and
renderToPipeableStream.
  • Loading branch information
gnoff committed Apr 19, 2024
1 parent 415ee0e commit 0d4e24b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/react-dom/src/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,17 +620,26 @@ describe('ReactDOMServer', () => {
describe('renderToStaticNodeStream', () => {
it('should generate simple markup', () => {
const SuccessfulElement = React.createElement(() => <img />);
const response = ReactDOMServer.renderToStaticNodeStream(
SuccessfulElement,
);
expect(response.read().toString()).toMatch(new RegExp('<img' + '/>'));
expect(() => {
const response =
ReactDOMServer.renderToStaticNodeStream(SuccessfulElement);
expect(response.read().toString()).toMatch(new RegExp('<img' + '/>'));
}).toErrorDev('ReactDOMServer.renderToStaticNodeStream() is deprecated', {
withoutStack: true,
});
});

it('should handle errors correctly', () => {
const FailingElement = React.createElement(() => {
throw new Error('An Error');
});
const response = ReactDOMServer.renderToStaticNodeStream(FailingElement);

let response;
expect(() => {
response = ReactDOMServer.renderToStaticNodeStream(FailingElement);
}).toErrorDev('ReactDOMServer.renderToStaticNodeStream() is deprecated', {
withoutStack: true,
});
return new Promise(resolve => {
response.once('error', () => {
resolve();
Expand Down Expand Up @@ -689,9 +698,7 @@ describe('ReactDOMServer', () => {
}

ReactDOMServer.renderToString(<Foo />);
expect(() =>
jest.runOnlyPendingTimers(),
).toErrorDev(
expect(() => jest.runOnlyPendingTimers()).toErrorDev(
'Warning: setState(...): Can only update a mounting component.' +
' This usually means you called setState() outside componentWillMount() on the server.' +
' This is a no-op.\n\nPlease check the code for the Foo component.',
Expand Down Expand Up @@ -719,9 +726,7 @@ describe('ReactDOMServer', () => {
}

ReactDOMServer.renderToString(<Baz />);
expect(() =>
jest.runOnlyPendingTimers(),
).toErrorDev(
expect(() => jest.runOnlyPendingTimers()).toErrorDev(
'Warning: forceUpdate(...): Can only update a mounting component. ' +
'This usually means you called forceUpdate() outside componentWillMount() on the server. ' +
'This is a no-op.\n\nPlease check the code for the Baz component.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ function renderToStaticNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
if (__DEV__) {
console.error(
'ReactDOMServer.renderToStaticNodeStream() is deprecated.' +
' Use ReactDOMServer.renderToPipeableStream() and wait to `pipe` until the `onAllReady`' +
' callback has been called instead.',
);
}
return renderToNodeStreamImpl(children, options, true);
}

Expand Down

0 comments on commit 0d4e24b

Please sign in to comment.