Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side rendering with next.js vs traditional SSR

I am very used to the approach where SSR meant that the page got a full refresh and received a full HTML from the server, where it gets rendered with razor/pub/other depending on the backend stack. So every time the user would click on the navigation links, it would just send a request to the server and the whole page would refresh, receiving a new HTML. That is the traditional SSR which I understand.

With SPA however, we have for example react or angular, where we receive almost empty HTML on the beginning and then the JS so that the whole app gets initialized on the client side. We can then have some REST API to get json data and render views on the frontend (client side routing and rendering) without any page refresh. We don't even need any server really.

Now, what I have a problem understanding is how SSR (such as next.js) works with react.

From what I am reading, the first request returns full HTML+CSS (which helps with SEO etc - I get that), but what happens later? What happens after that first/initial request? Does the whole react app initialize in the browser and then it just behaves EXACTLY as if it was a normal SPA (meaning we have client side routing and rendering from now on, without any need to make requests to that server)? In other words, does next.js still make any server requests after the initial one, or does it act like a typical SPA with CRA from now on?

I spent lots of time reading but all the articles mainly focus on the initial request and SEO and time to first byte, paint etc. and I am simply trying to understand why its called SSR since it seems to work different than the traditional SSR which I described on the beginning.

like image 211
marc08 Avatar asked Feb 09 '20 00:02

marc08


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.

When should I use server-side rendering NextJS?

Use Server-Side Rendering: Next. js pre-renders a page on each request. It will be slower because the page cannot be cached by a CDN, but the pre-rendered page will always be up-to-date.

Can I use NextJS without server-side rendering?

Use next/dynamic importsYou 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.

Does NextJS use SSR by default?

Remember, we learned that Next. js does static site generation by default. It simply works out of the box. However, it will try to detect which pre-rendering method you're using for each page.


2 Answers

does next.js still make any server requests after the initial one, or does it act like a typical SPA with CRA from now on?

You got it right. The first (initial) request is handled by the server and after that the frontend handles the routing (at least in the case of Next.js).

If you want to see an example OpenCollective is built with Next.js. Try playing around with it and see the Network tab in the DevTools.

I am simply trying to understand why its called SSR since it seems to work different than the traditional SSR which I described on the beginning.

It is called SSR because the app is effectively being rendered on the server. The fact that frontend routing takes over after the initial render doesn't remove the fact that the server did the work of rendering the app as oppose to the user machine.

like image 164
Johnny Zabala Avatar answered Oct 12 '22 08:10

Johnny Zabala


That's Not all the things that happen with Next.js, In Next.js You can build something called Hybrid apps.

In traditional SSR, all of your client requests get handled by the server. Every request goes to the server and gets responses. In classic CSR with something like React, as you said, all the things happens in the browser via the client-side javascript.

But, in Next.js you can define three different approaches (mainly two according to the docs) for pages to get delivered. based on the app needs and requirements, you can serve a couple of pages in pure traditional SSR mode, a couple of them in classic CSR mode, and a couple of in SSR mode via dynamic data that get fetched and rendered into the pages on the fly.

these features bring lots of flexibility to design a web-app that behaves perfectly in every different scenario that is needed.

like image 36
Shahryar Rp Avatar answered Oct 12 '22 07:10

Shahryar Rp