Sub topic 3
Checking local state before going to the network
This chapter assumes you know what a URL is, that browsers fetch pages over HTTP, and that HTTP responses carry headers. Once the address bar has turned your input into a navigation, the browser does not send a request right away. It first checks what it already knows. Each check can skip or shorten the steps that follow.
The order of local checks
The browser consults several pieces of local state before it opens any connection.
- HSTS list. HTTP Strict Transport Security (HSTS) is a policy that tells the browser to use only HTTPS for a host. If the host is on the list, the browser rewrites
http://tohttps://internally, before any request is sent. - HTTP cache. The browser looks for a stored response for this URL. A fresh entry can be used with no network traffic at all.
- Connection pool. The browser may already have an open connection to this host and port, which lets it skip DNS, TCP and TLS.
- DNS caches. If a new connection is needed, the browser and the operating system check their stored name-to-address answers. This is covered in the DNS chapter.
HSTS upgrades
Suppose you type http://example.com/ and the host is in the HSTS list. The browser changes the scheme itself and never sends a plain-text request. In DevTools this appears as an internal redirect with status 307 Internal Redirect. No server produced it.
A host enters the list in two ways. It can send the response header Strict-Transport-Security: max-age=31536000 over HTTPS, or it can be preloaded, meaning it ships in a list built into the browser. A preloaded host is protected even on your first visit.
The HTTP cache check
The browser keys the cache mainly on the URL. If it finds a stored response, it compares the response's age with its freshness lifetime. Chapter 10 covers the headers that set this lifetime. There are three outcomes:
| Cache state | What the browser does | Network cost |
|---|---|---|
| Fresh | Uses the stored response directly | None |
| Stale, with a validator | Sends a conditional request with `If-None-Match` or `If-Modified-Since` | One round trip, small response if unchanged (`304`) |
| Missing | Sends a normal request | Full request and response |
A reload changes this behavior. A normal navigation uses fresh entries. A regular reload revalidates the main resource. A hard reload skips the cache entirely. The "Disable cache" checkbox in DevTools has a similar effect while DevTools is open.
Reusing connections
Opening a TCP connection and a TLS session costs several round trips. The browser keeps recent connections open and reuses them for later requests to the same origin. With HTTP/2 and HTTP/3, one connection can also serve other host names that share the same IP address and certificate. If a reusable connection exists, the request skips the DNS, TCP and TLS stages.
Common errors
- Assuming "I changed the server, so the browser must see it." A fresh cached copy means the server is never contacted.
- Debugging with a warm cache. Test first loads in a private window or with the cache disabled.
- Expecting HSTS to be undone by changing the server. The browser remembers the policy until
max-ageexpires. Settingmax-age=0over HTTPS is the way to clear it. - Expecting a missing DNS lookup to mean DNS is broken. A reused connection or a cached address legitimately skips it.
Exercise
You type http://shop.example/ for a host that sent Strict-Transport-Security: max-age=31536000 last week. In DevTools you see a 307 Internal Redirect to https://shop.example/, and the HTTPS response is served from cache with no network activity. Which two local checks produced this result, and which later stages were skipped?
Show answer
The HSTS check upgraded the scheme to HTTPS before any request was sent, which is why the 307 is internal and no server was contacted. The HTTP cache check then found a fresh stored response and used it. Because no request was needed, DNS, TCP, TLS and the HTTP request were all skipped.