You have followed a request across the network, through the handshake, to the server and back. Now look at the other end: the program that asked, and the thing it asked for. The program is the browser. The thing is the web page. This lesson opens the browser and shows what a page is made of, and what loading a page actually means.
Difficulty: Beginner · Know this first: F25 — Virtualization: One Machine, Many Machines
Every step charges a little time. The bar below is a worked example for this lesson's scenarios, not a measurement from a live system. The shape is honest: the document is small and the pictures are the load, and a broken script swamps everything else. Step through the lesson or switch the scenario and watch the bar change.
Extra diagram · from scratch to master
Start with a bare document. Add one ingredient at a time until the whole page is on the map, broken image included.
Extra diagram · from scratch to master
Start with the address bar. Add one moment at a time until the page is ready, blank page included.
Build a page and watch it load. Pick how many images and scripts it has and how big it is, and the simulator counts the requests and adds up the load time. The rules are exact for this simulator, and the shape is the lesson: pictures are the load, scripts are the risk.
Each image is its own request and costs about 10 ms to fetch in this teaching example.
Each script is its own request and costs about 25 ms to run in this teaching example.
The document plus its stylesheet. Every 500 KB costs about 100 ms to download in this teaching example.
Try this: (1) 6 images, 3 scripts, 500 KB. Requests: 1 document + 1 stylesheet + 6 images + 3 scripts = 11. Load time: 200 ms base + 60 ms images + 75 ms scripts + 100 ms size = 435 ms. (2) 0 images, 0 scripts, 50 KB. Requests: 2. Load time: 200 + 0 + 0 + 10 = 210 ms. (3) 30 images, 10 scripts, 5000 KB. Requests: 42. Load time: 200 + 300 + 250 + 1000 = 1750 ms.
Three questions. No score is saved anywhere — this is just you against the material.
I take the same idea four times here. First I learn what it is and why it exists. Then I learn to use it and read what it tells me. Then I design with it so one failure cannot take the system down. Last I run it for the world.
Definition: A browser is a program that asks for web pages, reads them, and shows them. A page is HTML for structure, CSS for look, and JavaScript for behavior. The HTML document arrives first, and the pictures, fonts, and scripts it names arrive after it, each as its own request.
Types:
Uses: I use this when a page looks wrong. I ask which of the three languages is at fault before I touch the server.
Dimensions: At low weight a page loads fast and the three languages stay out of each other's way. What breaks first is a heavy page, and I notice it in the pictures arriving late. It costs nothing to learn the three-way split. It costs hours to debug a page without it. The trade is simple: a page is many requests, and every request costs time.
Definition: The browser reads the HTML top to bottom and builds the DOM, the model of the page in memory. It fetches the stylesheet and the scripts as it finds them, paints as it goes, and runs the scripts. The load event fires when the document and its named pieces are in place. First text, then shape, then pictures, then behavior.
Types:
Uses: I use the browser's network view at this level. I count the requests, I read their sizes, and I find the one that holds the page back.
Dimensions: As a page grows, the request count grows with it, and the slowest request sets the pace. What breaks is a single large script or image, and I notice it in the network view before users describe the symptom. It costs the habit of counting requests instead of guessing. The trade is that every ingredient buys something the page needs, and every ingredient costs a request.
Definition: At this level I make pages fast on purpose: fewer requests, smaller images, cached stylesheets, scripts that load without blocking the paint, and fonts that do not hold the text hostage. I measure the moments that matter: first text, first paint, and the moment the page answers.
Types:
Uses: I use this when a page is slow and the server is fine. I shrink the page, not the server, because the page is where the time goes.
Dimensions: At ten times the traffic the server is still fine and the page is still heavy, because the weight is per user, not per server. What breaks is the page that grew one image at a time, and I notice it in the load time, not in the server logs. It costs a budget per page: so many requests, so many kilobytes, no more. The trade is that every feature buys weight, and the budget is the only thing that keeps the weight honest.
How big is the page. A page that looks small can be heavy, and a heavy page is slow on a slow connection. I fetch the page with curl and count the bytes. The number is the size of the page alone, before images and scripts, and it is the floor for the real load time.
$ curl -s https://example.com | wc -c
577
$ curl -s https://example.com | grep -o '<title>[^<]*</title>'
<title>Example Domain</title>
Every flag in the commands above, letter by letter.
curl -s: -s means silent. It hides the progress meter.wc -c: -c counts bytes. The page is 577 bytes. Without -c, wc prints lines, words, and bytes all at once.grep -o: -o prints only the matching part of the line, not the whole line. Without it, grep prints the full line the title sits on, which is the whole page on one line.grep -m 1 stops after the first match. A page has one title, so -m 1 says so. Without it, grep keeps scanning the rest of the page for a second title that is not there.Check it: run curl -s https://example.com | grep -o '<title>[^<]*</title>', then the same line without -o. The first prints the title tag. The second prints the whole page.
Try it: run both commands. The page is 577 bytes, and the title is inside it. Now fetch a page you use every day and count it. If it is over a megabyte of HTML alone, the page is heavy before a single image loads.
Gzip on the wire. A server can compress a page before sending it, and the browser uncompresses it on arrival. I check with curl, asking for gzip and saving the raw bytes. The compressed page is smaller, and the difference is time saved on every load.
$ curl -s https://example.com -o plain.html
$ curl -s -H "Accept-Encoding: gzip" https://example.com -o gz.html
$ wc -c plain.html gz.html
577 plain.html
400 gz.html
Every flag in the commands above, letter by letter.
curl -o plain.html: -o saves the body to a file. The name after -o is the filename. Without -o, the body prints to the screen.curl -H "Accept-Encoding: gzip": -H sends a header with the request. This one asks the server to compress the answer. The server sends 400 bytes instead of 577.wc -c plain.html gz.html counts the bytes in both files, so the saving is visible: 577 against 400.curl -O saves with the remote name. -o picks the name. -O takes the name from the URL. curl -O https://example.com/index.html saves a file called index.html.Check it: run the three commands above, then run curl -s -O https://example.com/index.html in an empty folder and list it. A file called index.html appears, 577 bytes.
Try it: run the three commands. The plain page is 577 bytes. The gzipped page is 400 bytes. That is a third smaller, and the saving grows with the page. If the two files are the same size, the server is not compressing, and that is a fix worth making.
Definition: At this level the browser is a platform: it stores data, runs apps offline, enforces security rules about what a page may do, and isolates each site from the others. I design pages that work when the network is slow, degrade gracefully when a piece fails, and never trust a script I did not write. The page is an app, and the browser is its operating system.
Types:
Uses: I use this when a page must work for everyone, including users on slow networks and old devices. I test the page with the network throttled, not just on fast office Wi-Fi.
Dimensions: At a hundred times the users the page is still one document per user, and the cost is still per page. What breaks is the page that assumes a fast network and a new browser, and I notice it from users I never hear from, because they simply leave. It costs testing on slow networks and old devices, and the discipline to design for them. The trade is that a page built for the worst network works everywhere, and a page built for the best network works only there.
The page that looks small and is not. The HTML is only the start. A page pulls in styles, scripts, and images, and each one is a separate request. I count the requests by reading the page, not by guessing. A page with fifty requests is fifty chances for one of them to be slow.
$ curl -s https://example.com | wc -c
577
$ curl -s --compressed https://example.com | wc -c
577
Every flag in the commands above, letter by letter.
curl --compressed: --compressed asks for a compressed response and decompresses it before counting. The byte count matches the plain page, 577, because the count happens after decompression.-H "Accept-Encoding: gzip" saves compressed bytes, and wc -c counts those, 400. The two commands count different things, and the flag is the difference.Check it: run curl -s https://example.com | wc -c, then curl -s --compressed https://example.com | wc -c. Both print 577. Then run the -H form from the box above and watch wc -c print 400.
Try it: run both commands. The first counts the raw bytes. The second counts the bytes after curl uncompresses them. Both say 577, because the page is tiny. On a real page, the compressed number is the size that crossed the wire, and the raw number is the size the browser works with. The gap between them is the compression saving.
The DOM is the page as the browser understands it. HTML is text. The DOM is a tree of nodes in memory: the heading node, the paragraph node, the image node. Scripts read and change the DOM, and the browser repaints what changed. When a page updates without a refresh, a script changed the DOM and the browser painted the difference.
Render blocking is the rule that makes pages wait. The browser will not paint until it has the stylesheet, because painting without the paint would flash the wrong look and then repaint. A slow stylesheet holds the whole page back. That is why stylesheets are small and cached, and why a script in the head holds the body back until it runs.
The cache is the reason the second visit is faster. The stylesheet, the images, and the scripts are stored on your machine after the first visit, with expiry dates from their headers. The second visit asks for the document and reuses the rest. A page that never caches its pieces taxes every visitor on every visit.
A cookie is small memory with a job. The server sets it, the browser stores it, and the browser sends it back on every request to that site. It is how the server remembers you between requests. It is also why a cookie that grows large is a tax on every request, because it travels every time.
At 10x the traffic, the shape holds and the numbers grow. The document stays small. The pictures stay the load. I watch the request count per page, because it grows quietly, one image at a time.
At 100x, the design changes. The page is served from the edge, close to the user, and the pieces are cached there too. The browser still does the same work: read, paint, run, fill in. The edge just makes the fetching faster.
<!-- a minimal page, in full (teaching example) -->
<!doctype html>
<html>
<head><title>Hi</title></head>
<body><h1>Hello</h1><p>One page.</p></body>
</html>
What the snippet means: this is a whole web page. The doctype says it is HTML. The head holds the title, which shows in the tab. The body holds the content: a heading and a paragraph. There is no CSS and no JavaScript, so the browser paints it in its default look. Every page you have ever loaded is this shape, with more pieces hung on it.
Try it: weigh a page. Fetch example.com with curl and count the bytes. The answer is 577. Then fetch it asking for gzip and count the saved file. The answer is 400. Two numbers, and they tell you the page size and the compression saving. Do the same for a page you use daily, and you will see why some pages feel instant and others feel heavy.
$ curl -s https://example.com | wc -c
577
$ curl -s -H "Accept-Encoding: gzip" https://example.com -o gz.html
$ wc -c gz.html
400 gz.html
A team's homepage went blank for everyone, all at once. The server was up. The document arrived. The page was white, and nothing on it responded. The team was sure the site was down. The site was not down. The page was waiting.
Three things. First, no third-party script may block our page. They load late, or not at all, and the page never waits for them. Second, our own script now has a timeout: if a dependency does not answer in two seconds, the page carries on without it. Third, the on-call check for the homepage now loads the page in a browser and waits for it to paint, because a server that answers and a page that shows are two different things. The document was never the problem. The waiting was.
Design a news homepage that must show its first headline in under one second on a slow mobile connection. The page has a headline, a picture, a menu, and a list of ten stories.
Start with the count. The first screen needs the document, the stylesheet, and one picture: three requests. The menu and the ten stories are text in the document, not separate requests. The comments script loads after the page is usable, not before. The top picture is sized for a phone screen, about 100 KB, not the 2 MB original. The budget is one second: about 300 ms for the document, 200 ms for the stylesheet, and 500 ms for the picture, on a slow connection.
Loading the picture first buys a complete first screen and costs the time to fetch it, so the picture is small and the text appears before it. Deferring the comments buys a fast page and costs comments that arrive a second later, which nobody notices. Caching the stylesheet buys fast repeat visits and costs a stale look for a day when the design changes. And the one-second budget is for the first headline, not the whole page: the rest can arrive after, because a reader reads the headline first.