Skip to content
FrontDepth, Home

2016 – today

SSR, SSG and meta-frameworks

The server renders the React app to HTML, the browser shows it at once, and hydration makes the same components interactive.

MiddleRuns on: React 19.3.0, React DOM 19.3.0

The problem

Single-page apps sent an almost empty HTML page: nothing appeared until the JavaScript bundle had downloaded, run and fetched its data. On a slow phone that took seconds, and crawlers or link previews that do not run JavaScript saw a blank page. React could already render to a string on the server, but every team had to wire webpack, Babel, a Node server, routing and data loading together on its own.

The idea

The same components run in two places. On the server, renderToString() turns the React tree into HTML, and the props it was rendered with go into the page as JSON. The browser shows that HTML at once, then downloads the JavaScript and calls hydrateRoot() with the same component and the same props: React walks the existing DOM, attaches the event handlers and takes over. In between, the page looks ready but does not respond, the "uncanny valley": here about 50 ms, on a slow phone it can be seconds.

Meta-frameworks packaged this. In Next.js (2016) every file in pages/ is a route rendered on the server, and a page's static async getInitialProps() loads its data before rendering. Static site generation (Gatsby, Next.js getStaticProps) runs the same render once at build time instead of on every request.

What hurt next

Pages now arrived rendered, but the components were still classes: one feature's logic was spread over componentDidMount, componentDidUpdate and componentWillUnmount, sharing stateful logic needed higher-order components or render props, and every handler had to be bound to this (see the constructor). Hooks (React 16.8, 2019) gave function components state and effects that can be shared as custom hooks.

Hydration had its own cost: the browser downloads and runs the code of the whole page, even of parts that never change, and the data travels twice, as HTML and as JSON. Islands and server components later went after that.

Next era: Hooks

How close this is to the original

Runs React 19.3.0: renderToString on the simulated server and hydrateRoot (React 18) in the browser. Next.js 1.0 (October 2016) ran React 15.3: its server called getInitialProps and renderToString and put the props in an inline script that set __NEXT_DATA__ (Next.js 8 moved them into a JSON <script> like the one here), and its client called ReactDOM.render on the server's markup. React 16 added ReactDOM.hydrate for that, React 18 replaced it with hydrateRoot, and React 19 removed both ReactDOM.hydrate and ReactDOM.render. The class component is written as it was then. In Next.js getInitialProps also ran in the browser on client-side navigation; with a single page it only runs on the server here. Since React 18, renderToString still works but is discouraged in favour of the streaming APIs.

The todo app in this era