Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate renderToStaticNodeStream #28872

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions packages/react-dom/src/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,16 +580,28 @@ 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 and will be removed in an upcomingrelease of React',
{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 and will be removed in an upcomingrelease of React',
{withoutStack: true},
);
return new Promise(resolve => {
response.once('error', () => {
resolve();
Expand All @@ -614,12 +626,18 @@ describe('ReactDOMServer', () => {
throw promise;
}

const response = ReactDOMServer.renderToStaticNodeStream(
<div>
<React.Suspense fallback={'fallback'}>
<Suspender />
</React.Suspense>
</div>,
let response;
expect(() => {
response = ReactDOMServer.renderToStaticNodeStream(
<div>
<React.Suspense fallback={'fallback'}>
<Suspender />
</React.Suspense>
</div>,
);
}).toErrorDev(
'ReactDOMServer.renderToStaticNodeStream() is deprecated and will be removed in an upcomingrelease of React',
{withoutStack: true},
);
await resolve();
expect(response.read().toString()).toEqual('<div>resolved</div>');
Expand Down
13 changes: 13 additions & 0 deletions packages/react-dom/src/server/ReactDOMLegacyServerNodeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,23 @@ function onError() {
// Non-fatal errors are ignored.
}

let didWarnAboutDeprecatedRenderToStaticNodeStream = false;

function renderToStaticNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
if (__DEV__) {
if (!didWarnAboutDeprecatedRenderToStaticNodeStream) {
didWarnAboutDeprecatedRenderToStaticNodeStream = true;
console.error(
'ReactDOMServer.renderToStaticNodeStream() is deprecated and will be removed in an upcoming' +
'release of React. Use ReactDOMServer.renderToPipeableStream() and wait to `pipe` until the `onAllReady`' +
' callback has been called to produce a document suitable for static use cases.',
);
}
}

function onAllReady() {
// We wait until everything has loaded before starting to write.
// That way we only end up with fully resolved HTML even if we suspend.
Expand Down