Server side rendering with next.js vs traditional SSR

我与影子孤独终老i 提交于 2020-06-17 12:57:17

问题


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.


回答1:


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 care 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.



来源:https://stackoverflow.com/questions/60132559/server-side-rendering-with-next-js-vs-traditional-ssr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!