Skip to content

From URL to pixels

BasicsUnder the hood

Between pressing Enter and seeing a page, the browser runs the same stages every time, in the same order. Here they are one message at a time, from the DNS lookup to the first pixel, with what each one costs.

01 What the browser reads in a URL

The browser first decides whether you typed a URL or a search. A URL is then split into parts, and each part feeds a different stage.

https://shop.example/cart?item=7
PartValueUsed by
SchemehttpsConnect and TLS: port 443, encrypted
Hostshop.exampleDNS, then the TLS certificate check
Path/cartThe HTTP request line
Query?item=7Sent with the path; the server reads it

Before anything goes on the network, the browser also checks its HTTP cache. If it holds a fresh copy of the page, every network stage below is skipped. Assume here that it does not.

02 The whole trip, one message at a time

A first visit to https://shop.example/, whose page links one stylesheet. The top row is the stage in progress. The list below it is every message on the network, in order. A round trip is one message there and its reply back, the unit that network delay is paid in.

First visit to https://shop.example/
Step 1 of 15
Pixels none yet
Stages, in order
DNS
Connect
TLS
Request
Parse
Style
Layout
Paint
On the wire (→ sent, ← received)
Empty
  • stage running
  • done, or sent earlier
  • message just sent
  • stage not reached

You press Enter on https://shop.example/. The browser splits it into scheme https, host shop.example and path /. Nothing has been sent yet.

0 round trips

203.0.113.10 is an address reserved for documentation. With HTTP/3 the connection runs over QUIC, which does the transport and TLS handshakes together in one round trip, so Connect and TLS merge.

03 Each stage in one line

What each stage does and what slows it
StageWhat happensSlow when
DNSHost name becomes an IP addressThe name is not cached and the resolver is far away
ConnectTCP handshake with the serverThe server is far away: one full round trip
TLSKeys agreed, certificate checkedTLS 1.2 (two round trips) or a long certificate chain
RequestHTTP request out, response backThe server is slow to generate the page
ParseHTML into the DOM, CSS into the CSSOMScripts in the head block the parser
StyleComputed style for every elementVery many elements or complex selectors
LayoutSize and position of every boxLarge pages, or layout forced again and again
PaintPixels drawn, layers compositedBig areas, heavy shadows and filters

The first four stages are mostly waiting on the network, so their cost is set by distance. The last four run on your machine, so their cost is set by the page.

04 See it yourself

curl can report when each network stage finished. The times are seconds since the start, so each one includes the stages before it.

Shell
curl -o /dev/null -s -w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nfirst byte %{time_starttransfer}\ntotal %{time_total}\n' https://example.com/
Text
dns 0.013151
connect 0.020288
tls 0.037298
first byte 0.055441
total 0.055547

One real run; yours will differ. Subtract neighbours to get each stage: here TCP took about 7 ms, TLS about 17 ms and the server about 18 ms. For the rendering stages, open the browser’s DevTools: the Network panel’s Timing tab splits each request the same way, and the Performance panel shows Parse, Style, Layout and Paint as separate blocks.

05 What the second visit skips

First visit4 round trips to first byteDNS, TCP, TLS and the request all have to happen before any HTML arrives.
Revisit, connection still open1 round tripThe DNS answer is cached and the connection is reused, so only the request itself goes over the network.
Revisit, fresh in the HTTP cache0 round tripsWith a Cache-Control lifetime that has not run out, the browser uses its copy and goes straight to parsing.

Even with a new connection, TLS session resumption can make the TLS stage cheaper. Rendering, though, is never skipped: every visit still parses, styles, lays out and paints.

06 When the order bites

http://shop.example → https://shop.example/Redirects repeat the tripEach redirect is a whole extra request and response, often on a new connection with its own handshakes. Link to the final URL; HSTS lets the browser go straight to https on later visits.
<script src="app.js"></script> in <head>Scripts that stop the parserA plain script makes the parser stop, fetch it and run it before reading more HTML. Use defer (run after parsing) or async (run when loaded) unless the script must run first.
<link rel="stylesheet" href="/app.css">CSS holds back the first paintThe browser will not paint until the stylesheets in the head have loaded, so a slow CSS file means a blank page. Keep critical CSS small and served from the same origin.
fonts.example, cdn.example, api.exampleEvery new origin pays againEach other host needs its own DNS lookup, TCP and TLS. <link rel="preconnect" href="https://cdn.example"> starts that work early.

07 Check yourself

Question

A first visit over TCP with TLS 1.3 and HTTP/2, nothing cached. How many round trips before the first byte of HTML, counting DNS?

Question

The HTML has fully arrived but the screen stays blank until app.css loads. Why?

Question

You visit the same site again a minute later and the page is fresh in the HTTP cache. Which stages still run?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?