Introduction
There is a very famous question, “What happened when you input a URL and press Enter in the browser?”
Answer
1. URL parsing
When you input a URL in the browser, the browser will parse the URL into several parts:
- Protocol:
http
orhttps
- Host/Domain:
www.example.com
- Port:
80
or443
- Path:
/page.html
- Query:
?name=Philip
- Fragment:
#section1
2. DNS lookup
The browser will check the cache to see if the DNS lookup result is cached. If not, it will send a DNS query to the DNS server to get the IP address of the host.
- Check browser cache.
- Check OS cache.
- Check router cache.
- Check ISP(Internet Service Provider) cache.
- Make request to the DNS server.(Only if all cache above failed!)
3. TCP connection
The browser will establish a TCP connection with the server using the IP address and port number. This including the TCP handshake process. 关于TCP的连接与关闭,可以看这篇
4. HTTP request
The browser will send an HTTP request to the server. The request includes the following information:
- Request method:
GET
,POST
,PUT
,DELETE
, etc. - Request headers:
User-Agent
,Accept
,Cookie
, etc. - Request body: for
POST
andPUT
requests. - URL:
http://www.example.com/page.html?name=Philip#section1
5. Server processing
The server will process the request and generate a response. The response includes the following information:
- Status code:
200
,404
,500
, etc. - Response headers:
Content-Type
,Content-Length
, etc. - Response body: HTML, CSS, JavaScript, etc.
- Cookies:
Set-Cookie
header.
6. Browser rendering
The browser will render the response HTML, CSS, and JavaScript to display the web page to the user. When browser parse html page, it may download js file embed in html, this process might block the parsing, see here for details.
- Parse HTML: The browser will parse the HTML to create the DOM tree.
- The file was transferred from the server to the browser as binary data.
- The browser will parse the binary data to the .html file.
- The browser constructs the DOM tree based on the parsed HTML.
- If the html contains external CSS files, the browser will download the css in parallel, this won’t block the DOM construction.
- If the html contains external JavaScript files, the browser will download the js in parallel, this will/won’t block the DOM construction. see here for details.
- Parse CSS: The browser will parse the CSS to create the CSSOM tree.
- Render tree: The browser will combine the DOM tree and CSSOM tree to create the render tree.
- Layout: The browser will calculate the position and size of each element in the render tree.
- Paint: The browser will paint the pixels on the screen.
- Composite: The browser will composite the layers to display the final web page.
- JavaScript execution: The browser will execute the JavaScript code to add interactivity to the web page.
- Event handling: The browser will handle user events like click, scroll, etc.
Conclusion
- External CSS doesn’t block the DOM construction, but might block the render tree construction since the browser needs to wait for the CSSOM tree to be constructed.
- External JavaScript might block the DOM construction if and only if:
- The Scripts is in
<head>
tag, it will block the DOM construction. - And the script doesn’t set
async
ordefer
.
- The Scripts is in
- If the script is at the end of the
<body>
tag, it won’t block the DOM construction even when it doesn’t setasync
ordefer
.