
Before writing HTML, CSS or React, it helps to understand what actually happens between typing a URL and seeing a page. This knowledge explains why pages are slow, why caching matters, why HTTPS exists and how the frontend talks to the backend. It is also one of the most common opening questions in frontend interviews: "What happens when you type a URL into the browser and press Enter?"
This is Part 1 of the Learn Frontend series. We walk through DNS, TCP and TLS, HTTP requests and responses, status codes, caching, cookies and browser storage, and how a frontend calls APIs.
1. What happens when you type a URL and press Enter
- URL parsing: the browser splits
https://example.com/products?id=7into the scheme (https), host (example.com), path (/products) and query (?id=7). - DNS lookup: the host name is translated into an IP address, checking the browser cache, the OS cache and then DNS servers.
- Connection: the browser opens a TCP connection to that IP (or QUIC for HTTP/3) and performs a TLS handshake to set up encryption for HTTPS.
- HTTP request: the browser sends a request such as
GET /products?id=7with headers (cookies, accepted formats, caching info). - Server response: the server (or a CDN close to the user) returns a status code, headers and the HTML body.
- Parsing and sub-resources: the browser parses HTML into the DOM and discovers CSS, JavaScript, images and fonts, requesting each one.
- Rendering: the browser builds the CSSOM, combines it with the DOM, calculates layout, paints pixels and composites layers onto the screen (covered in depth in Part 9).
- JavaScript and interactivity: scripts run, attach event listeners and may fetch more data from APIs.
2. DNS: turning names into addresses
Computers connect using IP addresses, but people remember names. The Domain Name System is a distributed directory that maps names to addresses. A lookup checks caches first (browser, operating system, router) and then asks a recursive resolver, which walks from the root servers to the .com servers to the domain's own name servers.
| Record | Purpose |
|---|---|
| A / AAAA | Name to IPv4 / IPv6 address |
| CNAME | Alias of another name (for example www to the root domain or a CDN) |
| MX | Mail servers for the domain |
| TXT | Text, often used for domain verification |
Each record has a TTL (time to live) saying how long it may be cached. Frontend performance tip: <link rel="dns-prefetch"> and <link rel="preconnect"> start DNS lookups and connections to third-party origins early.
3. HTTP requests, responses and status codes
HTTP is a request-response protocol. Every request has a method, a path, headers and sometimes a body; every response has a status code, headers and usually a body.
GET /api/products/7 HTTP/1.1
Host: shop.example.com
Accept: application/json
Cookie: session=abc123
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache
ETag: "v42"
{"id": 7, "name": "Keyboard", "price": 2499}| Method | Meaning | Safe / idempotent |
|---|---|---|
| GET | Read a resource | Safe and idempotent |
| POST | Create something or trigger an action | Neither |
| PUT | Replace a resource | Idempotent |
| PATCH | Partially update a resource | Not guaranteed |
| DELETE | Remove a resource | Idempotent |
| Status range | Meaning | Common examples |
|---|---|---|
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 4xx | Client error | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests |
| 5xx | Server error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable |
HTTP/1.1 sends one request at a time per connection, so browsers open several connections. HTTP/2 multiplexes many requests over one connection and compresses headers. HTTP/3 runs over QUIC (UDP), avoiding head-of-line blocking when packets are lost.
4. HTTPS and TLS
HTTPS is HTTP inside an encrypted TLS connection. During the TLS handshake the server presents a certificate signed by a trusted certificate authority, proving it owns the domain; the two sides then agree on session keys. TLS gives:
- Confidentiality: nobody on the network can read passwords or cookies.
- Integrity: nobody can modify the page in transit, for example to inject ads or scripts.
- Authentication: you are really talking to the domain in the address bar.
Modern browser features such as service workers, geolocation and HTTP/2 in practice require HTTPS, and browsers mark plain HTTP pages as "Not secure".
5. Caching, cookies and browser storage
HTTP caching avoids downloading the same files again. The server controls it with headers:
Cache-Control: max-age=31536000, immutablefor versioned files likeapp.3f9a.js: cache for a year.Cache-Control: no-cacheplus anETagfor HTML: the browser revalidates, and the server replies 304 Not Modified if nothing changed.- A CDN stores copies of files on servers near users, cutting latency.
| Cookies | localStorage | sessionStorage | |
|---|---|---|---|
| Size | About 4 KB each | About 5 MB per origin | About 5 MB per origin |
| Sent to server | Yes, with every matching request | No | No |
| Lifetime | Until expiry or session end | Until cleared | Until the tab closes |
| Readable by JavaScript | Unless HttpOnly | Yes | Yes |
Store session tokens in HttpOnly, Secure, SameSite cookies rather than localStorage, so injected scripts cannot read them (more in Part 14).
6. How the frontend talks to the backend
The browser loads the frontend (HTML, CSS, JavaScript), and the JavaScript then calls APIs on servers to read and change data, usually exchanging JSON over HTTPS. A REST API maps resources to URLs (/api/users/42) and actions to HTTP methods.
async function loadProduct(id) {
const res = await fetch('/api/products/' + id, {
headers: { Accept: 'application/json' },
credentials: 'include', // send cookies
});
if (!res.ok) { // fetch does not throw on 404 or 500
throw new Error('Request failed with status ' + res.status);
}
return res.json();
}When the API lives on a different origin (scheme, host or port), the browser applies CORS rules: the server must send headers such as Access-Control-Allow-Origin, and some requests trigger a preflight OPTIONS request first. CORS is enforced by browsers to protect users, and it is a frequent source of "it works in Postman but not in the browser" bugs.
- Typing a URL triggers DNS lookup, TCP/TLS connection, an HTTP request, the response, parsing, rendering and script execution.
- DNS maps names to IP addresses and is heavily cached; preconnect and dns-prefetch speed up third-party origins.
- Know HTTP methods, status code ranges and the differences between HTTP/1.1, HTTP/2 and HTTP/3.
- HTTPS (TLS) provides confidentiality, integrity and authentication.
- Use Cache-Control and ETags for caching, HttpOnly cookies for sessions, and understand CORS when calling APIs.
Practice problems
Try these hands-on exercises in your browser's DevTools (press F12) to see everything in this article for yourself.
- Open the Network tab, reload any site and find the HTML document request. Note its status code, size and timing breakdown (DNS, connect, TLS, waiting).
- Reload again and spot files served from memory or disk cache, and any 304 Not Modified responses.
- Click a JavaScript or CSS file and read its Cache-Control header. Is it long-lived and versioned?
- Check the Protocol column (enable it by right-clicking the column headers) to see h2 or h3.
- In the Application tab, inspect cookies and look for the HttpOnly, Secure and SameSite flags.
- Run fetch('https://api.github.com/users/octocat').then(r => r.json()).then(console.log) in the console and inspect the response headers.
- Try fetching a different site's page from the console and read the CORS error message.
Interview questions and FAQ
What is the difference between HTTP and HTTPS?
HTTPS is HTTP sent over an encrypted TLS connection. It prevents eavesdropping and tampering and proves the server's identity with a certificate. Plain HTTP sends everything in readable text.
What is the difference between a 401 and a 403 status code?
401 Unauthorized means the request lacks valid authentication, so the user should log in. 403 Forbidden means the user is authenticated (or authentication would not help) but is not allowed to access the resource.
What is the difference between cookies, localStorage and sessionStorage?
Cookies are small, sent automatically with requests and can be made HttpOnly. localStorage and sessionStorage are larger, stay in the browser and are never sent automatically; localStorage persists until cleared, sessionStorage only for the tab.
What is CORS and why does it exist?
Cross-Origin Resource Sharing is a browser mechanism that blocks a page from reading responses from another origin unless that server explicitly allows it with headers. It protects users from malicious sites using their logged-in sessions on other sites.
How does HTTP/2 improve performance over HTTP/1.1?
HTTP/2 multiplexes many requests on one connection, compresses headers and allows prioritisation, so bundling tricks like domain sharding and sprite sheets are less necessary.
Comments
Post a Comment