AJAX → SSR, SSG and meta-frameworks
1999 – today · 2016 – today
What changed
- Where the state lives
AJAX
On the server: the list is server data; the page keeps the last copy it received, and the filter.
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.- How the page is rendered
AJAX
The server sends data (JSON), not HTML; the page builds the list items with
document.createElement.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.- What happens on a change
AJAX
Each change is a background request; its callback replaces the page's copy of the list and redraws only the list, the counter and the filter buttons.
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.- How the code is organised
AJAX
A page script with a request helper and handlers attached in
window.onload, talking to a server API that answers JSON.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.
Why it changed
What hurt in each era along the way, and the era that came next.
AJAX→jQuery
Every browser did it a little differently. Internet Explorer 5 and 6 created the object with
new ActiveXObject("Microsoft.XMLHTTP"), Internet Explorer before version 9 attached events withattachEventinstead ofaddEventListener, and each browser had its own DOM quirks, so scripts grew long and full of browser checks. Libraries like jQuery wrapped XHR, events and the DOM in one short API that worked the same in every browser.jQuery→MVC in the browser (Backbone)
When the data lives only in the DOM, every feature reads and writes the page directly: the counter counts
<li>elements, the filter checks classes. As apps grew, handlers changing the page from many places became hard to follow ("jQuery spaghetti"), and there was no model of the data to test, save or reuse. Libraries like Backbone moved the data into models and kept the page in views.MVC in the browser (Backbone)→Two-way binding (AngularJS)
Backbone left the wiring to you. Every view subscribed to its models by hand, chose what to redraw and read form fields back itself, so much of the code only connected data to the DOM. A view thrown away without
remove()stayed subscribed and kept reacting as a "zombie view", and re-rendering a template replaced elements, losing focus and half-typed text. AngularJS answered with two-way data binding: the HTML declares what shows what, and the framework keeps the page and the data in sync.Two-way binding (AngularJS)→React and the virtual DOM
Every binding added a watcher, and every digest checked all of them, at least twice whenever something had changed, so pages with many bindings slowed down on each click or keystroke (1.3 added one-time bindings,
::, to cut the count). Two-way bindings and watchers that change other values made it hard to see what changed what, and code outside AngularJS, like asetTimeoutcallback or a jQuery plugin, had to remember to call$scope.$apply().Angular 2 was a new framework rather than the next version: an app could not just upgrade, and the official path was to migrate it piece by piece, with both frameworks running side by side. React answered with one-way data flow: a component renders from its state, and a virtual DOM works out what to change on the page.
React and the virtual DOM→Flux and Redux
State that several components need has to live in their closest common parent and travel down as props through every component in between, even those that only pass it on, while callbacks travel down the same way to change it. In a large app,
setStatecalls in many components, triggered by clicks, server responses and timers, all changed what the screen showed, so it was hard to follow why it looked the way it did. Flux and Redux moved the shared state out of the components into a store that changes only through actions.Flux and Redux→Component frameworks (Vue, Angular, Svelte)
Every small feature needed the same pieces in several files: an action type constant, an action creator, a case in a reducer, and
mapStateToProps/mapDispatchToPropsto connect a component, plus objects copied by hand so that nothing was mutated. Setting up a store with middleware and the DevTools took more code still. Redux itself later answered with Redux Toolkit; component frameworks like Vue, Angular and Svelte offered reactive state you change directly and HTML templates that update themselves, with much less ceremony.Component frameworks (Vue, Angular, Svelte)→SSR, SSG and meta-frameworks
These apps still rendered in the browser. The server sent an almost empty page, and nothing useful appeared until the framework and the app's JavaScript had downloaded and run; crawlers and link previews that do not run JavaScript saw nothing. Routing, server rendering, code splitting and data loading were left to each team to put together. Meta-frameworks such as Next.js and Nuxt bundled those pieces around the component framework and rendered pages on the server or at build time.
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.
AJAX
plain browser APIs, no library
/src/main.js · 97 lines
// AJAX era: the page talks to the server in the background with XMLHttpRequest and
// redraws only the list. The page itself is never loaded again.
// The list belongs to the server; the page keeps the last copy it received.
var todos = [];
var filter = "all";
// Every request goes the same way: open, send, then wait until readyState is 4 (done).
function request(method, url, body, callback) {
var xhr = new XMLHttpRequest();
// true = asynchronous: the page stays usable while the request travels.
xhr.open(method, url, true);
if (body) xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
};
xhr.send(body);
}
// Every answer is the whole list as JSON: keep it and redraw.
function show(list) {
todos = list;
render();
}
// One call per todo gives each handler its own todo. A handler created inside render()'s
// for loop would close over the loop's single var and see its value from the end of the loop.
function makeItem(todo) {
var li = document.createElement("li");
var label = document.createElement("label");
var box = document.createElement("input");
var title = document.createElement("span");
box.type = "checkbox";
box.checked = todo.done;
box.setAttribute("data-toggle", "");
box.onclick = function () {
request("POST", "/todos/" + todo.id + "/toggle", null, show);
};
// A text node: what the user typed never becomes HTML.
title.className = "title";
title.appendChild(document.createTextNode(todo.title));
label.appendChild(box);
label.appendChild(document.createTextNode(" "));
label.appendChild(title);
li.appendChild(label);
if (todo.done) li.className = "done";
return li;
}
// Only the list, the counter and the filter buttons change; the rest of the page stays.
function render() {
var list = document.getElementById("todo-list");
var buttons = document.getElementById("filters").getElementsByTagName("button");
var left = 0;
var i;
list.innerHTML = "";
for (i = 0; i < todos.length; i++) {
if (!todos[i].done) left++;
if (filter === "all" || (filter === "done") === todos[i].done) {
list.appendChild(makeItem(todos[i]));
}
}
document.getElementById("todo-count").innerHTML = left + " left";
for (i = 0; i < buttons.length; i++) {
buttons[i].className = buttons[i].getAttribute("data-filter") === filter ? "selected" : "";
}
}
// Handlers are attached from the script when the page has loaded, not written into the HTML.
window.onload = function () {
var field = document.getElementById("new-todo");
var buttons = document.getElementById("filters").getElementsByTagName("button");
var i;
document.getElementById("todo-form").onsubmit = function () {
var title = field.value.replace(/^\s+|\s+$/g, "");
if (title !== "") {
request("POST", "/todos", "title=" + encodeURIComponent(title), show);
field.value = "";
}
// false cancels the normal submission: no new page, the request runs in the background.
return false;
};
for (i = 0; i < buttons.length; i++) {
// Inside the handler, this is the button that was clicked.
buttons[i].onclick = function () {
filter = this.getAttribute("data-filter");
render();
};
}
// The page arrives without the list; the first request fetches it.
request("GET", "/todos", null, show);
};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>
);
}
}