Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is server-side rendering compatible with single-page applications?

My problem is that I'm unable to understand how server-side rendering single-page application frameworks like Next.js receive prerendered, full HTML on the front end without having to rewrite the entire page. For example, the nextjs website states the following:

By default, Next.js pre-renders every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript. Pre-rendering can result in better performance and SEO. Each generated HTML is associated with minimal JavaScript code necessary for that page. When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive. (This process is called hydration.)

I understand how this bolsters the responsiveness of an SPA on first page load. But after that first load, what makes server-side rendering compatible with SPAs? I think this arises from a fundamental misunderstanding that I can't catch, so here are some further questions I have that might help you to catch it:

  1. Do SSR SPAs always respond with full prerendered HTML, or only for first page loads?
  2. If the former is true, then on subsequent responses, how does the client efficiently render only the difference rather than rewriting the whole page?
  3. Otherwise, if the latter is true, then how does an SSR SPA backend tell when it's responding to a first request, when the response should be the whole HTML, versus a subsequent request, when the bulk of the page is already there and all that needs to be sent is some relatively minimal information?

What am I misunderstanding about what makes SSR compatible with SPAs?

Many thanks in advance to everyone who tackles this question!

like image 768
Lenny Zhou Avatar asked Mar 31 '21 18:03

Lenny Zhou


People also ask

How does server-side rendering work?

Server-side rendering means using a server to generate HTML from JavaScript modules in response to a URL request. That's in contrast to client-side rendering, which uses the browser to create HTML using the DOM. Server-side rendering with JavaScript works similarly to other server-side languages such as PHP or .

How does angular server-side rendering work?

Angular Universal executes on the server, generating static application pages that later get bootstrapped on the client. This means that the application generally renders more quickly, giving users a chance to view the application layout before it becomes fully interactive.

How does server-side rendering React work?

What is Server-Side Rendering? Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements.


1 Answers

Welcome to Stackoverflow :)

Usually SSR is used for initial rendering of the page, so for the first question - for the first page load

This is necessary, so the SPA will be more SEO-compatible (there also might be some performance improvements with this, but it's usually secondary goal) and Search Engine bots will be able to parse pages without the need for JS

The SSR usually has several important steps:

  1. Server render
  2. Sending of rendered data to browser
  3. Hydration. Hydration - is a ReactJS (since we're talking about next.js here) 'function' that binds the server-rendered HTML to the React on the Frontend. So basically binds server-rendered DOM to virtualDOM

After the hydration step you basically have a fully-functional normal SPA, which has it's own routing and able to fetch data on itself.

Usually you have different endpoint on the BE to fetch the data and to render the page. So basically the rendering process on the BE is somewhat similar to what you have on the FE - your application backend fetches the data from separate endpoints, applies all of the logic and renders the app.

Btw, to ensure that SSR works properly, there is a principle called 'Isomorphic code' - i.e. if you're using a library for data fetching, it has to support both node.js and browser APIs. That's why, for example, you'd have to use Next.js own Router when you have a Next.js application - it just works on both FE and BE unlike react-router, which would require some additional steps to achieve that

like image 171
Nausik Avatar answered Oct 18 '22 08:10

Nausik