liveBook · Manning

Server-side rendering

As explained in React in Depth

Server-side rendering is the process of producing application interface output on a server before sending it to the client.

Server-side rendering (SSR) runs React on the server to produce a page’s HTML with its data already in place. A client-only Vite application instead initially serves an effectively empty root element; the visitor must wait for JavaScript and data before seeing the page. SSR can shorten that wait, especially on slow connections or devices, but its speed advantage depends on successful hydration. section 11.1.2 section 11.5

Think of a route as a React component plus a data package. On the first page of a visit, the server combines them into HTML and sends the JavaScript application too. In the framework flow described here, later page visits request only new route data because the browser already has the application. Keeping data loading beside the component also makes it possible to fetch from a database or external API on the server without putting server-only secrets in browser code. section 11.1.2 section 11.1

Figure 11.2 Imagine a page to display as the simplest puzzle ever. It has only two pieces: a component defined in React that defines the template and a data package that contains the data required to turn the template into HTML. In a classic React setup, we retrieve those two pieces in two separate requests and put them together in the browser when everything is loaded. In the framework setup, we get both pieces at the same time instantly.
figure

The component-and-data diagram contrasts assembling a page in the browser with sending their combined HTML from the server. section 11.1.2

A route with server-loaded data

For /shop/shoes, read categoryId from the URL, load its category, and render the category name in a React component. A Next.js pages route puts getServerSideProps beside its default-exported component and returns the category as props; a Remix route uses a loader and reads the result with useLoaderData(). Both examples redirect when the category is missing. The files and data APIs differ, but the component-plus-data arrangement is the same. section 11.3.2

Hydration is the constraint to check before treating faster initial HTML as a win: React expects the browser’s first render to match the server-rendered output. A mismatch can impose a performance penalty that outweighs the initial gain. Server rendering also rules out reading browser-only local storage on the server; cookies can be an alternative for relatively small data items. Choose a framework and data flow with those constraints in mind, rather than assuming SSR alone guarantees a faster site. section 11.1 section 11.5

How React in Depth covers it

How deep it goes
Explains first-load and subsequent-navigation data flow, then shows route-data implementations; leaves framework details beyond the examples aside.
Examples
Compares an empty Vite root with server-generated HTML and implements a parameterized shop-category route in Next.js and Remix.
What it assumes
Assumes React components and browser-side applications; introduces the component-plus-data model used for server-rendered routes.
What you can do after
Apply it via Next.js getServerSideProps or a Remix loader to deliver route data in initial HTML while checking hydration constraints.
How it connects
Connects SSR to React website frameworks, route data, URL routing, hydration, cookies, and client-side rendering.

Also explained in

Compare how 8 books explain server-side rendering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.