Sub topic 2
From keystrokes to a navigation
This chapter assumes you know what a URL is and can read the parts of one (scheme, host, port, path, query, fragment). It covers what the browser does between your first keystroke and the moment it decides to start a network request.
The address bar does two jobs
The address bar (the browser calls it the omnibox) accepts both URLs and search terms. The browser must decide which one you typed before it can do anything else. Typing example.com/docs should load a page. Typing best pasta recipe should run a search.
The browser applies a few rules in order:
- If the text starts with a known scheme such as
https://,http://orfile://, treat it as a URL. - If the text has no spaces and looks like a host name (for example
example.com,localhost:3000or an IP address), treat it as a URL and add a default scheme. - Otherwise, treat it as a search query and send it to the default search engine.
The exact rules differ between browsers. Some also check a list of known single-word hosts, such as localhost or intranet names, so that wiki can mean a site on your company network.
Both paths end the same way. A search is also a navigation, to a URL such as https://search.example/?q=best+pasta+recipe. The query text is percent-encoded so that spaces and special characters are safe inside a URL.
Normalizing the URL
Once the browser has a URL, it normalizes it. You can see the result with the standard URL class, which follows the same parsing rules the browser uses for links.
const url = new URL('https://example.com:8443/docs/intro?lang=en&ref=home#setup');
console.log(url.protocol); // 'https:'
console.log(url.hostname); // 'example.com'
console.log(url.port); // '8443'
console.log(url.pathname); // '/docs/intro'
console.log(url.search); // '?lang=en&ref=home'
console.log(url.hash); // '#setup'Normalization typically lowercases the scheme and host, removes the default port (:443 for HTTPS), and percent-encodes unsafe characters. The fragment (#setup) is kept in the browser. It is never sent to the server.
Suggestions and speculative work
While you type, the browser shows suggestions drawn from your history, bookmarks and the search engine. It may also start work before you press Enter. Common examples are a DNS lookup or a connection to the most likely host. This is speculative work, and it makes the page feel faster when the guess is right. It costs little when the guess is wrong.
Pressing Enter starts a navigation
A navigation is the browser's request to load a new document into a tab. Pressing Enter, clicking a link and calling location.assign() in JavaScript all start one. The browser first runs a few local steps:
- The page you are leaving may run
beforeunloadhandlers. A handler can ask you to confirm that you want to leave, for example when a form has unsaved edits. - If only the fragment changed (
/page#ato/page#b), the browser scrolls within the current document and makes no request. - Otherwise, the browser prepares a request and moves to the next stage: checking its own state before using the network.
Common errors
- Typing a bare host and getting a search. A name with a space or an unusual form, such as
my server, is treated as a search. Add a scheme (http://myserver) to force a navigation. - Expecting the fragment on the server. Code that reads
#setupon the server will never see it. Use the query string if the server needs the value. - Forgetting encoding. When you build URLs in code, use
URLandURLSearchParamsinstead of string concatenation, so that special characters are encoded correctly.
Exercise
You type localhost:3000/app#settings and press Enter. Which part of this input is not sent to the server, and why does the browser treat the input as a URL instead of a search?
Show answer
The fragment #settings is not sent. The browser uses it only inside the loaded page. The input is treated as a URL because it has no spaces and has the form of a host name with a port and a path, so the browser adds a default scheme and navigates instead of searching.