From baacaf0226d35163879036975e52946a9787038a Mon Sep 17 00:00:00 2001 From: Josh Story Date: Thu, 18 Apr 2024 18:33:53 -0700 Subject: [PATCH] Deprecate `renderToStaticNodeStream` (#28872) 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. --- .../__tests__/ReactServerRendering-test.js | 20 ++++++++++++++----- .../server/ReactDOMLegacyServerNodeStream.js | 7 +++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactServerRendering-test.js b/packages/react-dom/src/__tests__/ReactServerRendering-test.js index fed6988077187..2df9ad6e5dd1b 100644 --- a/packages/react-dom/src/__tests__/ReactServerRendering-test.js +++ b/packages/react-dom/src/__tests__/ReactServerRendering-test.js @@ -620,17 +620,27 @@ describe('ReactDOMServer', () => { describe('renderToStaticNodeStream', () => { it('should generate simple markup', () => { const SuccessfulElement = React.createElement(() => ); - const response = ReactDOMServer.renderToStaticNodeStream( - SuccessfulElement, - ); - expect(response.read().toString()).toMatch(new RegExp('')); + expect(() => { + const response = ReactDOMServer.renderToStaticNodeStream( + SuccessfulElement, + ); + expect(response.read().toString()).toMatch(new RegExp('')); + }).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(); diff --git a/packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js b/packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js index 25b88156e0755..9aef2f00fea44 100644 --- a/packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js +++ b/packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js @@ -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); }