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

1

14

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!

Antoinetteanton answered 31/3, 2021 at 18:33 Comment(1)
I think we don't have anything named SSR SPAs, single page applications are rendered client-side naturally. Nonetheless we can mix client-side rendering and server-side rendering.Slump
P
14

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

Palaeolithic answered 31/3, 2021 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.