SSR, SSG and meta-frameworks → Server components and islands
2016 – today · 2020 – today
What changed
- Where the state lives
SSR, SSG and meta-frameworks
In the page component's
this.state, started from the props the server loaded withgetInitialProps(); a copy of those props travels with the HTML as JSON.Server components and islands
In the island's
useState, started from the props the server serialized next to it. The static parts have no state at all.- How the page is rendered
SSR, SSG and meta-frameworks
The server renders the whole page to HTML with
renderToString(); the browser hydrates it withhydrateRoot()and renders every later change itself.Server components and islands
The server renders the whole page to HTML; only the island's element is hydrated with
hydrateRoot(). React never touches the static HTML.- What happens on a change
SSR, SSG and meta-frameworks
After hydration everything happens in the browser: a handler calls
this.setState()and React re-renders the page component. Nothing goes back to the server.Server components and islands
Inside the island a setter re-renders
TodoAppand React updates the DOM that differs; the rest of the page never changes.- How the code is organised
SSR, SSG and meta-frameworks
A page in
pages/written once for both sides, plus framework code: a server that renders and serializes, and a client entry that hydrates.Server components and islands
Server-only components that never reach the browser, around client islands that each have their own root, script and props.
Why it changed
What hurt in each era along the way, and the era that came next.
SSR, SSG and meta-frameworks→Hooks
Pages now arrived rendered, but the components were still classes: one feature's logic was spread over
componentDidMount,componentDidUpdateandcomponentWillUnmount, sharing stateful logic needed higher-order components or render props, and every handler had to be bound tothis(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.
Hooks→Server components and islands
Everything still ran in the browser: the page arrived empty, every component's JavaScript had to download, run and render before anything useful appeared, and server rendering hydrated the whole page even where nothing was interactive. Effects with dependency arrays and stale closures became a common source of bugs. Islands and React Server Components moved the non-interactive parts back to the server.
The same app, side by side
The main file of each version. Both apps pass the same test: same markup, same behaviour, different ideas.
The apps run in sandboxes in your browser.
SSR, SSG and meta-frameworks
React 19.3.0, React DOM 19.3.0
/pages/index.jsx · 101 lines
// A page as Next.js apps wrote it in 2016–2018: a class component with a static data method.
// The same file runs twice: on the server (to HTML) and in the browser (hydration).
import { Component } from "react";
const FILTERS = { all: "All", active: "Active", done: "Done" };
export default class TodoPage extends Component {
// Called on the server before the first render; what it returns becomes the page's props.
// A real page would read a database or an API here.
static async getInitialProps() {
return {
todos: [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false },
],
};
}
constructor(props) {
super(props);
// The first state comes only from props, so the server and the browser render the same HTML.
this.state = { todos: props.todos, filter: "all", title: "" };
this.nextId = Math.max(0, ...props.todos.map((t) => t.id)) + 1;
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ title: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
const title = this.state.title.trim();
if (!title) return;
const todo = { id: this.nextId++, title, done: false };
this.setState((state) => ({ todos: [...state.todos, todo], title: "" }));
}
toggle(id) {
this.setState((state) => ({
todos: state.todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)),
}));
}
render() {
const { todos, filter, title } = this.state;
const shown = todos.filter((t) => filter === "all" || (filter === "done") === t.done);
const left = todos.filter((t) => !t.done).length;
return (
<main className="todo-app">
<h1>Todos</h1>
<form id="todo-form" onSubmit={this.handleSubmit}>
<input
id="new-todo"
value={title}
onChange={this.handleChange}
placeholder="What needs doing?"
autoComplete="off"
aria-label="New todo"
/>
<button id="add" type="submit">
Add
</button>
</form>
<ul id="todo-list">
{shown.map((todo) => (
<li key={todo.id} className={todo.done ? "done" : undefined}>
<label>
<input
type="checkbox"
data-toggle
checked={todo.done}
onChange={() => this.toggle(todo.id)}
/>{" "}
<span className="title">{todo.title}</span>
</label>
</li>
))}
</ul>
<div className="footer">
<span id="todo-count">{left} left</span>
<div className="filters">
{Object.entries(FILTERS).map(([name, label]) => (
<button
key={name}
type="button"
data-filter={name}
className={filter === name ? "selected" : undefined}
onClick={() => this.setState({ filter: name })}
>
{label}
</button>
))}
</div>
</div>
</main>
);
}
}Server components and islands
React 19.3.0, React DOM 19.3.0
/server.jsx · 48 lines
// Islands era: the server renders the whole page to HTML, and only the islands get JavaScript.
// An islands framework such as Astro does this for you: you only mark what is interactive.
import { renderToStaticMarkup, renderToString } from "react-dom/server";
import TodoApp from "./islands/TodoApp.jsx";
// The "database" lives on the server.
const todos = [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false },
];
// A server-only component: it runs here and leaves plain HTML behind. It ships NO JavaScript:
// this file is never sent to the browser, and nothing hydrates its HTML.
function Intro() {
return (
<>
<h1>Todos</h1>
<p>This heading and note are plain HTML with no JavaScript. Everything below them is an island.</p>
</>
);
}
// "<" is escaped so that a todo titled "</script>" cannot end the script element early.
function serialize(props) {
return JSON.stringify(props).replace(/</g, "\\u003c");
}
export async function handle({ method, path }) {
if (method !== "GET" || path !== "/") return { status: 404, body: "<h1>Not found</h1>" };
const props = { initialTodos: todos };
// Static part: HTML only, without the markers hydration needs. No JavaScript.
const intro = renderToStaticMarkup(<Intro />);
// The island: HTML that hydrateRoot can attach to, with its props as JSON right next to it.
const island = renderToString(<TodoApp {...props} />);
return {
status: 200,
body: `
<main class="todo-app">
${intro}
<div id="todo-island">${island}</div>
<script id="todo-island-props" type="application/json">${serialize(props)}</script>
</main>
<script src="/client.jsx"></script>`,
};
}