Sub topic 3
Checking local state before going to the network
The previous chapter ended with a parsed URL and a decision that the input is a navigation, not a search. This chapter covers what the browser does next, before it sends any packet. It consults state it already holds, and that state can rewrite the URL, answer the request entirely, or shorten the work that follows.
This chapter assumes you can read a URL into its parts and know that browsers keep caches. It also assumes you have used the Network panel in DevTools at least once.
Why look locally first
Every network round trip costs time, and the cost is highest for the first request, which blocks everything else. A browser that can skip or shorten that request makes the page faster. It also makes the page safer, because some of the local checks exist to prevent insecure requests.
Four kinds of local state matter at this point:
- The HSTS list, which says which hosts must be reached over HTTPS.
- Service workers, which are scripts that can answer requests themselves.
- The HTTP cache, which stores earlier responses.
- Reusable resources, such as cached DNS answers and open connections.
The exact order differs between browsers and versions. The sequence below matches what Chromium-based browsers do in the common case.
HSTS: upgrading to HTTPS before sending anything
HTTP Strict Transport Security (HSTS) is a policy that tells a browser to use only HTTPS for a host. A server sets it with a response header sent over HTTPS:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preloadmax-ageis how many seconds the browser remembers the policy. Here that is one year.includeSubDomainsextends the policy to every subdomain.preloadsignals that the site wants to be in the preload list.
The browser then has two sources of HSTS knowledge:
- Dynamic entries. The browser stores them after it sees the header on an HTTPS response. They expire after
max-age. - The preload list. This is a list of hosts shipped inside the browser itself. A host on it is protected on the very first visit, before the browser has ever seen the header.
When you type http://example.org/page, the browser checks the host against both. On a match it changes the scheme to https and, if there is no explicit port, uses 443. This happens inside the browser. DevTools shows it as a 307 Internal Redirect with a header like Non-Authoritative-Reason: HSTS. That status never appears on the wire.
This differs from a server-side redirect, which costs a full round trip and an unencrypted request. Without HSTS, a first request to http:// goes out in plaintext. An attacker on the same network can intercept it and prevent the redirect to HTTPS, which is called SSL stripping. HSTS closes that window for any host the browser already knows about.
Edge cases
- The header is ignored if it arrives over plain HTTP. Otherwise an attacker could inject it.
- If you set
max-age=0, the browser deletes the entry. This is how a site removes the policy. - With HSTS active, certificate errors cannot be clicked through. The browser shows a hard error with no "proceed anyway" option.
- HSTS is keyed by host name, not by IP address or port. An address like
http://192.0.2.10/is never upgraded. - Some browsers also try HTTPS first when you type a bare host name or an
http://URL for a host with no HSTS entry, then fall back to HTTP if that fails. That is a separate, newer feature and not part of HSTS.
Service workers: code that can answer for the network
A service worker is a JavaScript file that a site registers with the browser. Once active, it sits between pages in its scope and the network. If a navigation URL falls in a worker's scope, the browser starts the worker (or wakes it) and fires a fetch event. The worker's handler can then:
- Return a response from its own cache storage.
- Build a response in code.
- Call
fetch()and pass the request on, optionally changing it first.
When the worker returns a response without touching the network, the request ends there. This is how offline-capable web apps load with no connection.
This check comes before the HTTP cache, because the worker decides whether the HTTP cache is used at all. Starting a worker that is not running adds a small delay, so sites sometimes enable navigation preload. That lets the browser send the network request in parallel while the worker boots.
In DevTools, the Application panel lists registered workers. The Network panel marks responses served by a worker with "(ServiceWorker)" in the Size column.
The HTTP cache: reusing a stored response
If no worker handles the request, the browser looks in its HTTP cache. The cache key is based on the URL, and modern browsers add the top-level site as part of the key. This partitioning stops one site from learning which resources you loaded on another site. The practical consequence is that a shared library URL cached while visiting site A is not reused on site B.
A stored response is in one of three states:
| State | Meaning | What the browser does |
|---|---|---|
| Fresh | Its freshness lifetime has not run out | Uses it with no network request |
| Stale | The lifetime has run out | Sends a conditional request to check it |
| Absent | Nothing stored, or the entry was evicted | Sends a normal request |
Freshness comes from headers on the original response, for example:
HTTP/1.1 200 OK
Cache-Control: max-age=3600
ETag: "v42"For one hour after it was stored, the response is fresh. After that, the browser can ask the server whether it changed by sending If-None-Match: "v42". A 304 Not Modified reply with no body lets it reuse the stored copy. The header rules, directives like no-store and no-cache, and validation are covered in the chapter on HTTP caching. For now, remember that the cache check is local and costs almost nothing when it hits.
Two further points affect navigations:
- Reload behaves differently from entering a URL. A normal reload revalidates the main document, while a hard reload skips the cache for it and for its subresources.
- Back and forward navigation may restore a whole page from the back/forward cache (bfcache), a snapshot of the page in memory. In that case the browser does not reparse or rerun anything.
DNS cache and connection reuse
If the request must go to the network, the browser still avoids work it has already done:
- It checks its own host cache, then the operating system's resolver cache, for the host's IP address. A hit skips the DNS query entirely. The next chapters explain DNS in detail.
- It checks its connection pool for an open connection to the same origin. A reusable HTTP connection (kept alive, or multiplexed in HTTP/2 and HTTP/3) skips both TCP setup and the TLS handshake.
These show up as zero or missing phases in a request's timing. A request with no DNS, connect, or TLS segment reused existing state.
Observing the local checks
The Navigation Timing API records time spent in each phase. If everything before the request is served from local state, those phases collapse to zero or near zero. This snippet prints a summary for the current page, and you can paste it into the DevTools console:
const [nav] = performance.getEntriesByType('navigation');
console.log('Protocol:', nav.nextHopProtocol);
console.log('Service worker start ms:', nav.workerStart);
console.log('Fetch start ms:', nav.fetchStart);
console.log('DNS ms:', nav.domainLookupEnd - nav.domainLookupStart);
console.log('Connect ms:', nav.connectEnd - nav.connectStart);
console.log('Transfer size bytes:', nav.transferSize);
console.log('Body size bytes:', nav.decodedBodySize);
const cached = nav.transferSize === 0 && nav.decodedBodySize > 0;
console.log('Served from cache:', cached);A transferSize of 0 with a non-empty body means the document came from cache. A value of a few hundred bytes with a body means a 304 revalidation. workerStart is 0 when no service worker was involved.
A hands-on check you can do in a browser:
- Open DevTools, go to the Network tab, and tick "Preserve log".
- Visit a site that uses HSTS, but type its address with
http://. Look for the307 Internal Redirectrow at the top. - Reload normally, then reload again with "Disable cache" ticked. Compare the Size column, which reads "(memory cache)" or "(disk cache)" in the first case and a byte count in the second.
Common mistakes
- Treating the 307 from HSTS as something the server sent. It never left your machine, so server logs will not show it.
- Expecting
curlto upgradehttp://URLs.curlhas no preload list and ignores HSTS unless you give it an HSTS cache file, so it shows the real server redirect. - Debugging a "stale" page without checking for a service worker. A worker can serve old content no matter what the cache headers say. Use "Unregister" in the Application panel to rule it out.
- Testing performance with a warm cache and assuming users see the same. Real first visits have no cached entries, no DNS cache, and no open connections.
Exercise
You type http://shop.example/cart for a host that is in the browser's HSTS preload list. The server's own HTTP port redirects to HTTPS. Which of those redirects does the browser use, and what does DevTools show?
Show answer
Neither server redirect is needed. The browser upgrades the URL to https://shop.example/cart using its local HSTS knowledge, before any connection. DevTools shows a 307 Internal Redirect generated by the browser, and the plaintext HTTP request is never sent.
Exercise
A page's navigation timing shows transferSize of 0, decodedBodySize of 14000, and workerStart of 0. What happened, and what would you see if the entry had been stale but unchanged on the server?
Show answer
The document was served from the HTTP cache, with no service worker involved and no network use. If the entry had been stale and unchanged, the browser would have sent a conditional request, received a 304 Not Modified, and reused the stored body. transferSize would then be a small nonzero number covering only the headers.