How Browsers Work
Follow a page from the address bar to the screen: the requests it turns into, the trees the browser builds, the pipeline that paints them and the one thread your JavaScript shares with all of it. You finish able to read a waterfall, predict what a script logs and say why a page is blank.
- Beginner
- 4 units, 12 lessons, 156 exercises
- Text, HTML, JavaScript
- No experience needed; 11 helps
Unit 01 · 3 lessons
From URL to bytes
Follow a page from the address bar to the dozens of requests it turns into
- 01Navigation
What a URL says, what happens before the first byte, and redirects
- 02Finding the rest
How the parser discovers files, what blocks it, and defer against async
- BOSSRead a waterfall
One page, six requests: what waited on what, and which fix moves the finish
Read the unit guide
A URL is a set of instructions. The scheme, host and port say how and where to connect. The path and query are sent in the request. The fragment after # never leaves the browser: it only says where to scroll once the page is there.
Before the first byte of HTML arrives the browser needs a DNS lookup, a TCP handshake, a TLS handshake and one HTTP exchange (the Networking roadmap covers each). The moment that first byte arrives is the time to first byte, and nothing else about the page can start before it. A redirect (301, 302) answers with a Location header instead of a page, and the browser starts again from there, with a new connection if the host changed.
The Content-Type header decides what happens to the body. text/html goes to the HTML parser, which works on a stream: it builds the page from the bytes it has and asks for every other file the HTML names. One HTML response routinely becomes dozens of requests.
Images never hold the parser up. A plain <script src> does: the parser stops, the script downloads and runs, and only then does parsing continue. defer runs the script after the whole document is parsed, in document order. async runs it whenever it arrives, in arrival order. A script also waits for any stylesheet above it that is still loading.
While the parser is blocked, a preload scanner reads ahead in the raw HTML and starts downloads early. It only finds URLs written in the HTML. A font named in a stylesheet or an image added by a script is discovered late, after the file that names it has arrived.
A waterfall shows every request as a bar on a timeline. Read it for chains: a request that could only start once another had finished. The longest chain decides when the page is done, so breaking a chain usually beats making one file smaller.
Unit 02 · 3 lessons
From bytes to pixels
Trace how HTML and CSS become trees, boxes and pixels, and what holds up the first paint
- 01Two trees
HTML becomes the DOM, CSS becomes the CSSOM, and only some nodes get a box
- 02Layout, paint, composite
What each stage works out, and which CSS change makes which stages run again
- BOSSThe first paint
One page, four files: what must arrive before pixels, and the cheapest way to paint sooner
Read the unit guide
The browser never draws HTML. It parses the bytes into a tree of nodes, the DOM, where every element and every run of text is a node. The parser never gives up: it closes tags you left open and adds the html, head and body you left out. CSS is parsed into a second tree of rules, the CSSOM.
Five stages turn those trees into a frame, always in this order. Parse builds the trees. Style works out which rules apply to which node. Layout gives every box a size and a position. Paint fills in pixels, layer by layer. Composite stacks the layers on the screen.
Only what will be drawn gets a box. display: none takes an element and everything inside it out of layout, though all of it stays in the DOM. visibility: hidden and opacity: 0 keep the box and its space and only stop it showing.
A change does not always need every stage. Change a width and the browser lays out, paints and composites. Change a colour and it skips layout. Change transform or opacity on an element with its own layer and it can skip layout and paint, which is why animations use those two. At 60 frames a second each frame has about 16 ms for all of it.
The first paint has to wait for two things: the DOM for the content, and every stylesheet in the head, because a rule that arrives later may override an earlier one. A stylesheet blocks painting. A plain script blocks the parser, so nothing below it exists to be painted. Images block neither.
So the cheapest way to paint sooner is usually to stop something blocking: defer on a script, a media attribute on a stylesheet the screen does not need, or a small stylesheet written straight into the HTML.
Unit 03 · 3 lessons
JavaScript in the page
Predict when your code runs, where an event travels, and why a busy script freezes the page
- 01The event loop
The call stack, tasks and microtasks, and why a promise beats a zero timeout
- 02Events
Capture and bubbling, target, delegation, preventDefault and stopPropagation
- BOSSA frozen page
One slow click handler: why the page freezes, and three ways to give the thread back
Read the unit guide
A page has one main thread. It runs your JavaScript, and it also does style, layout and paint and answers every click and key press. Only one of those can happen at a time.
The event loop decides what runs next. Work arrives as tasks: a script, a timer firing, a click, a network response. The loop takes one task and runs it to the end. Nothing interrupts a running function. When the call stack is empty it runs every waiting microtask, which is where promise callbacks and the code after an await go, and only then may it paint and take the next task. So a resolved promise always beats a zero timeout. The Concurrency roadmap introduces the loop itself. Here you get the two queues and the page around them. Node and browsers agree on the order for every case in this unit.
A click is not delivered to one element. It travels down from the document to the target (capture), then back up through every ancestor (bubbling). Listeners run on the way up unless you ask for capture. event.target is where it started and event.currentTarget is the element whose listener is running. That lets one listener on a list handle every row, including rows added later: delegation.
preventDefault() cancels what the browser would do by itself, such as following a link or sending a form. stopPropagation() ends the journey. They are independent: each does only its own job.
A task that runs for more than about 50 ms is a long task. While it runs the page cannot paint or respond, and a text change made at its start is never seen. The cures are to split the work and give the loop a turn between pieces with a timer (a microtask does not help), to do visual updates in requestAnimationFrame, which runs once just before each paint, or to move pure computation to a web worker.
A worker is a second thread with no DOM. It gets copies of the data you post to it and posts results back. And defer changes when a heavy script runs, not how long it holds the thread.
Unit 04 · 3 lessons
The platform
Know what the browser stores, what it lets a page read, and how to ask it what went wrong
- 01Storage
Cookies and their attributes, localStorage, sessionStorage, and what travels with a request
- 02Origins
What an origin is, the same-origin policy, and CORS read as HTTP messages
- BOSSDebug a page
One shop page, three symptoms: it is blank, a request fails, a click does nothing
Read the unit guide
A browser keeps data for a site in a few places, and they differ in one way that matters most: who gets to see it. A cookie is set by the server with Set-Cookie and then sent back by the browser in the Cookie header of every request to that site, with no code involved. localStorage and sessionStorage are the opposite: only script on the page can read them, and they are never sent anywhere.
Cookie attributes narrow that down. HttpOnly hides the cookie from JavaScript while still sending it. Secure sends it over HTTPS only. SameSite limits when it goes along with requests that start on another site (the Security roadmap shows the attack this stops). Max-Age sets a lifetime, and without one the cookie goes when the browser closes.
localStorage stores strings per origin, shared by every tab and kept until cleared. sessionStorage belongs to one tab and goes with it. IndexedDB is a real database in the browser for large or structured data.
An origin is scheme, host and port together. The same-origin policy lets a page send requests almost anywhere but read responses only from its own origin. That is what stops a random page reading your mail with your cookies. Embedding an image or a script from elsewhere is allowed. Reading the response is not.
CORS is how a server opts in. The browser adds an Origin header, and the script may read the response only if it carries a matching Access-Control-Allow-Origin. For requests a plain form could never make (a DELETE, a JSON body, an Authorization header) the browser first sends an OPTIONS preflight and sends the real request only if the answer allows it. The browser enforces all of this. curl does not.
DevTools shows every stage of this roadmap. Elements is the live DOM and its styles. Console has script errors. Network has every request and its status. Performance shows where the main thread spent its time. Most page bugs are a question for one of those four.
Learn How Browsers Work on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.