Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
How the Web Works  ›  Capstone Project

Browsers — the Other End of the Wire

Browsers and how they work? 15 minA browser turns one URL into a DNS lookup, a connection and many HTTP requests, then parses HTML and CSS into a page and runs JavaScript that may call your API.

Browsers — the Other End of the Wire

Most requests your backend answers come from a browser, and a browser is busier than it looks. One visit to the bookshop's page can make a dozen requests to your servers. When you know what the browser does with each answer, you know which responses slow the page down.

The rule

One URL becomes many requests, then a tree, then pixels.

When a shopper opens https://bookshop.example/books, the browser:

  1. Finds the server: a DNS lookup turns bookshop.example into an IP address (unless it is cached).
  2. Connects: it opens a TCP connection, and because of https, sets up encryption with TLS.
  3. Requests the document: it sends GET /books and receives HTML.
  4. Parses and discovers: while reading the HTML it builds the DOM, a tree of elements. Each <link rel="stylesheet">, <script src> and <img> it meets becomes another request.
  5. Styles, lays out, paints: it combines the DOM with the CSS rules, works out where every box goes, and draws pixels.
  6. Runs JavaScript: scripts can change the DOM and call your API, for example with fetch("/api/books").
<link rel="stylesheet" href="/static/site.css">
<script src="/static/app.js" defer></script>
<img src="/covers/42.jpg" alt="Cover of The Salt Road">

Those three lines mean three more requests after the HTML arrives.

Browsers also enforce a safety rule that backend developers meet early: the same-origin policy. A script from https://bookshop.example may not read a response from a different origin, such as https://api.bookshop.example, unless that server says it is allowed with CORS headers like Access-Control-Allow-Origin.

The shortcut

Count the requests. For any page, list: one for the HTML, one per stylesheet, script and image, and one per API call its scripts make. Each item on the list is a request your servers or CDN must answer, and each can fail on its own.

Opening the bookshop's book listStep 1 of 5
  1. Find and connect

    DNS gives the IP address; the browser opens a TCP connection and completes the TLS handshake.

A trap: a slow stylesheet can hold up the whole page, because the browser usually waits for CSS before painting so it does not show unstyled content first.

Drills

Answer fast — trust the rule.

quick drill

A page's HTML links 1 stylesheet, 2 scripts and 5 images. With an empty cache, how many HTTP requests does loading it take at minimum?

quick drill

What does the browser build from the HTML it parses?

quick drill

For a site the browser has never visited, what must happen before it can send GET /books?

quick drill

A script on https://bookshop.example fetches from https://api.bookshop.example, and the browser blocks reading the response. What fixes it?

Remember

  • One URL becomes many requests, then a tree, then pixels.
  • Count the requests: HTML, each asset, each API call. Each one is work for your backend or CDN.
  • Different origins need the API server's permission through CORS headers.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe whole journey

Walk through everything that happens from typing https://bookshop.example/books to seeing a list of books that is loaded by JavaScript from /api/books. Point out each moment your backend is involved.