Building the page: on the server or in the browser

A web page can be built on the server or in the browser. The choice changes how fast the first screen appears and how much data travels.

You open a food delivery app in your browser and click through categories and menu items. On some sites the whole page goes blank and redraws after every click. On others only the list changes, and the header and the search box stay where they are.

Your browserShows the page
The app's serverSends what the page needs
Think first. Both kinds of site show the same menu. What must be different about what the server sends to your browser for each click?
On some sites the whole page reloads and on others it does not. Ask students to name one site of each kind and say how they could tell, for example by watching the browser tab for a loading spinner.

Read the two cards. Each one shows where the page is built and what travels when you click.

Server-rendered page

Every click fetches a whole new page

The server fills in a template and sends a complete page. The browser replaces the old one.

Single-page app

One page, then only data moves

The first load brings a shell and a large bundle of JavaScript. Later clicks fetch only data.

More detail

Server-rendered page, the older model

The server fills in a template (a page layout with blanks), produces finished HTML (the language that describes a web page), and sends a complete document. The browser discards the old page and draws the new one. Many older sites work this way.

click → request → server renders → full HTML → repaint first screen: fast, the HTML arrives ready to draw each later click: another full page

Single-page app, the newer model

The first load fetches an almost empty shell and a large bundle of JavaScript (code that the browser runs). After that, each click fetches JSON, and the browser updates only the parts of the DOM that changed. The DOM is the browser's live model of the page. Nothing else reloads.

first load → shell + bundle → fetch JSON → build DOM first screen: slower, nothing shows until the bundle runs each later click: a few KB of JSON and no redraw of the rest

The same two menu items can be sent as JSON, XML or HTML. Select each format and read its size in bytes. The blue part of each bar is the data itself and the gray part is packaging.

More detail

The text of each format

The information is the same each time: six values, 32 characters of data. Only the packaging differs. Each size counts the text with its line breaks and indentation removed. HTML is the format for web pages, and XML wraps every value in named tags.

A student opens your app, views some pages and leaves. Drag the slider to change how many pages they view, and watch which model has moved less data.

How much do they look at?
More detail

The numbers behind the bars

First screen
—
Total transferred
—
Ahead
—

The model uses 45 KB of HTML per server-rendered page, a 280 KB bundle for the single-page app, and 4 KB of JSON per navigation after the first load. Stylesheets and scripts are counted once because the browser caches them, which means it keeps a copy.

Most people leave after one or two pages. Drag the slider there and check which model wins. A single-page app also suits a brochure site (a site of a few fixed pages) poorly.

Decide which kind of project yours is. If it is mostly pages that people read, server-rendered pages have fewer parts and a faster first screen. If people use it for twenty minutes, clicking around, a single-page app makes up its download cost after about seven pages.

You can now choose between the two ways of building a page for a project.

More detail

What most student projects do

Most student projects are the first kind and get built as the second kind, because tutorials show that approach. Django (a Python web framework) builds server-rendered pages by default. Choose based on how long users stay and how many pages they open.

What each choice costs elsewhere

Rendering moves to the user's device

A single-page app has the browser build the page, so your server no longer does that work. That raises the capacity ceiling covered in Server capacity, using hardware you did not buy.

→ See Server capacity. It uses the user's battery.
The first screen is slower

Server-rendered HTML is ready to draw when it arrives. A bundle has to download, load and run before anything appears, and then it fetches its data.

→ See Round trips and latency. The cost is now on the client.
You rebuild what the browser did for free

The back button, scroll position, the address bar and opening a link in a new tab all work by default on server-rendered pages. A single-page app has to implement each one.

→ Easy to miss until it breaks
XML is heavier than JSON

XML adds rules (schemas) and labels (namespaces and attributes) that JSON does not have, so it is larger. Banks and government systems still use it, for example in SOAP messages, which are an XML message format.

→ See Payments and gateways. Larger on the wire, with stricter rules.