Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React SSR instead of 'getInitialProps' of 'next.js'

For improving TTFB (time to the first byte), the 'PageSpeed Insights ' suggests using ReactDOMServer.renderToNodeStream(), but I don't know how to implement it. I read the rendertonodestream article, but I don't know how to use it. Also, I read the dev.to article, but in my next.js app, there is no webpack.config.js file. If I cannot use react renderToNodeStream with Next.js, how can I cover the effect of renderToNodeStream in Next.js?

like image 416
S. Hesam Avatar asked Apr 19 '20 04:04

S. Hesam


People also ask

Should I use NextJS without SSR?

Next. js does not work without server-side rendering (SSR) by default. I preferred using a non-SSR solution like Create React App, when my app did not require SSR, because SSR had caused me so many unnecessary problems. Turns out Next is still 💯/💯, even if you only need index.

Is getInitialProps deprecated?

on Sep 21, 2020. getInitialProps is not deprecated. But we do not recommend its usage over getStaticProps or getServerSideProps . That's doesn't mean you can not use it, it only means that from our opinion you should use the new data fetching methods.

Can I use NextJS without server-side rendering?

Use next/dynamic imports You can use the ssr: false object to disable server-side rendering of your dynamically imported component, as shown below. Or else, you can create a wrapper component called NonSSRWrapper and then enclose any page in that component to disable SSR.

What is getInitialProps in NextJS?

getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data already populated from the server. This is especially useful for SEO. getInitialProps will disable Automatic Static Optimization.

What is getinitialprops in ReactJS?

getInitialProps is an async function that can be added to any page as a static method. Take a look at the following example: getInitialProps is used to asynchronously fetch some data, which then populates props. Data returned from getInitialProps is serialized when server rendering, similar to what JSON.stringify does.

Does getinitialprops work on both server and client?

Tech 10. Sep. 2019 JAMstack, Next.js, and React Following the tutorials, getInitialProps is meant to work “on both server and the client”, which is totally correct.

What does getinitialprops disable?

getInitialProps will disable Automatic Static Optimization. getInitialProps is an async function that can be added to any page as a static method. Take a look at the following example: getInitialProps is used to asynchronously fetch some data, which then populates props.

How to get the path of the page in getinitialprops?

getInitialProps receives a single argument called context, it's an object with the following properties: pathname - Current route. That is the path of the page in /pages asPath - String of the actual path (including the query) shown in the browser err - Error object if any error is encountered during the rendering


1 Answers

renderToNodeStrem() This function returns a readable stream. With this function we still get requests from the browser, we make our initial api requests then we build a tiny snippet of our react application so we attempt to render application. But we just render the bare minimum of it like the first component, the first bit of the HTML. The instant we get that first little tiny bit of HTML ready, we send that snippet to the browser. Then on the server we put together next tiny little snippet of our HTML. we then take that tiny snippet and we send it to the browser and then we repeat the same process many times. So with this function we are assembling tiny pieces of our html document and sending them over to the user’s browser.

TTFB: Time To First Bite This is the number that we really care about when it comes to search engine optimization. It is a duration of how long it took for our server to put together some initial bit of HTML and send a response back to the browser. It is very widely used by Google and other search engines to rate the performance of our page. This is far short in duration with the renderToNodeStream() because we are building a tiny bit of HTML and sending it.

if you start to render your application with nodeStream and then you come along a component that needs to redirect, you cannot do redirecting. Because once you start piping all that content from the nodeStream into the res object, that instantly starts the response and sends it back to the user. Imagine users come along with a page that needs to be authorized but it is not, you have to redirect the user with status code change but you cannot change the status code.

If you want to use the renderToNodeStream you have to change the entire architecture of the app. instead of next routing, you have to use react-router dom. For react server side, you have to use StaticRouter which passes the context prop to all of the components. It is similar to what getInitialProps "context" object. If you are going to use react router dom, then you have to look for all the components and decide what async operations have to be completed before sending the response object. You have to this manually.

Technically you cannot use react renderToNodeStream with next.js. INstead you have to configure webpack, build your own server, most likely express server and implement everything on your own.

Next.js is popular because there is alot of work to complete the server-side rendering. However as of now, next.js has no support for renderToNodeStream. but it is on the way.

like image 113
Yilmaz Avatar answered Sep 21 '22 04:09

Yilmaz