question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

question: why don't use renderToNodeStream instead of renderToString?

See original GitHub issue

Hi guys, I love this boilerplace, very clean. But I have a question about react-dom/server

Why donā€™t we use renderToNodeStream instead of renderToString?

Rendering to a stream can reduce the time to first byte (TTFB) for your content, sending the beginning of the document down the wire to the browser before the next part of the document has even been generated. All major browsers will start parsing and rendering the document earlier when the content is streamed from the server this way.

import { renderToNodeStream } from "react-dom/server"
import MyPage from "./MyPage"app.get("/", (req, res) => {
  res.write("<!DOCTYPE html><html><head><title>My Page</title></head><body>");
  res.write("<div id='content'>"); 
  const stream = renderToNodeStream(<MyPage/>);
  stream.pipe(res, { end: false });
  stream.on('end', () => {
    res.write("</div></body></html>");
    res.end();
  });
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Ridcommented, Aug 16, 2019

Iā€™ve made a fork to test this here: https://github.com/Rid/react-cool-starter

However there are a few issues:

  1. Flickering on initial render as the CSS has to be loaded after renderToNodeStream due to CSS extraction
  2. Weā€™re unable to minify the html as you need the complete rendered html in order to minify

Youā€™re welcome to fork my fork and see if you can address these issues some other way.

1reaction
hienhuynhtmcommented, Aug 23, 2019

@Rid Iā€™ll check your fork, then see that I can fix or not. Anyway, thanks for your support. @wellyshen your project structure & coding standard looks great, I love it šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it ok to use ReactDOMServer.renderToString in the ...
I have used it and now my event handlers are not called. By rendering the component in js object rather than dom. are...
Read more >
Upgrading to React 18 on the server Ā· Discussion #22
However, it's not deprecated, so you can keep using renderToString . In React 18, we're adding very limited <Suspense> support to renderToStringĀ ...
Read more >
ReactDOMServer
The following methods can be used in the environments that don't support streams: ... Instead, use renderToNodeStream on the server and ReactDOM.
Read more >
ReactDOMServer | Working and Examples of Dom Server ...
This method is just like renderToString, the only difference is that the additional DOM attributes are not created which is used internally by...
Read more >
Streaming Server-Side Rendering and Caching at Spectrum
Using renderToNodeStream. The API for streaming server-side rendering isn't all that different from the standard renderToString API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found