Rendering a web page – step by step
Have you ever thought about what happens when you surf the web? It's not as simple as it seems:
- You type an URL into address bar in your preferred browser.
- The browser parses the URL to find the protocol, host, port, and path.
- It forms a HTTP request (that was most likely the protocol)
- To reach the host, it first needs to translate the human readable host into an IP number, and it does this by doing a DNS lookup on the host
- Then a socket needs to be opened from the user's computer to that IP number, on the port specified (most often port 80)
- When a connection is open, the HTTP request is sent to the host
- The host forwards the request to the server software (most often Apache) configured to listen on the specified port
- The server inspects the request (most often only the path), and launches the server plugin needed to handle the request (corresponding to the server language you use, PHP, Java, .NET, Python?)
- The plugin gets access to the full request, and starts to prepare a HTTP response.
- To construct the response a database is (most likely) accessed. A database search is made, based on parameters in the path (or data) of the request
- Data from the database, together with other information the plugin decides to add, is combined into a long string of text (probably HTML).
- The plugin combines that data with some meta data (in the form of HTTP headers), and sends the HTTP response back to the browser.
- The browser receives the response, and parses the HTML (which with 95% probability is broken) in the response
- A DOM tree is built out of the broken HTML
- New requests are made to the server for each new resource that is found in the HTML source (typically images, style sheets, and JavaScript files). Go back to step 3 and repeat for each resource.
- Stylesheets are parsed, and the rendering information in each gets attached to the matching node in the DOM tree
- Javascript is parsed and executed, and DOM nodes are moved and style information is updated accordingly
- The browser renders the page on the screen according to the DOM tree and the style information for each node
- You see the page on the screen
- You get annoyed the whole process was too slow.
I, too, get annoyed when the above steps take longer than one tenth of a second. But now at least I have some documentation to look at, while waiting the remaining fractions of a second before the page renders.
Spoiled we are, all of us.
(Feel free to add more steps, through the comments…)
Comments
By: madr (#1)
Further frustration:
Stylesheets are parsed as they turns up, and can create a flash of white screen when not placed in .
The same goes for scripts: it doesn't wait until all other stuffs have been downloaded.
Scripts blocks pararell downloads (step 3-15) for all resources until it's happy. That's because people still use document.write these days.
Lovely!
By: Roland Bouman (#2)
By: Craig Buchek (#3)
By: Loon (#4)
By: Cody (#5)
By: Kristopher (#6)
By: Robin (#7)
By: Pariloto (#8)
I think that a "correct" html combined with a good browser will get you happy as all the steps above will be made under the tenth of a second that you talked about.
By: Case pariuri (#9)
By: Markus (#10)
By: Mac (#11)
By: Gkanell (#12)
By: Guillermo (#13)
By the way, sometimes here in Montevideo, with mobile ADSL it seems that we are still in that era. That's why I prefer information over spectacularity: ¡avoid heavy pages!
Great article, very helpful, ¡Gracias!
Guillermo.
By: Siegfried (#14)
1. Not all web pages are built out of a database. So you may skip this steps for all socalled static web pages. I think constructing static pages dynamically is nonsense.
2. The page is rendered earlier. At least in modern browsers. As soon as the html is loaded and parsed, the page is rendered (while loading other resources like stylesheets). After loading the stylesheets the page is rendered again. And after loading each image the page is rendered again. And after loading and parsing the javascript the page is rendered again.
By: R2J WebWorld (#15)
By: jeffatrackaid (#16)
By: Rodrigo Alves Vieira (#17)
By: washburn (#18)
we have come a far way
By: moocr (#19)
I just develop a very simple blowser using java, it can not display any image or picture, it just show the links, hehe, I study the steps by this way!
By: Hasan Sheikh (#20)