Journey of a web page - A mind map !
Tags: webdev, webperf, architecture, html
Published: 2022-11-06 07:33:38


Ever wondered what happens when you hit enter after typing a url in the browser’s address bar? 🤔


![what-happens-visually-on-hitting-enter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zn5sojo1gjr4oslt7i1.png)



Oh, I wish!😨 
The browser BTS does so many things for this to appear so seamless. And understanding those are very important to make our applications highly performant.

![You got this](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfhxxdaoq73flt3ju8pt.png)


Let's understand these one by one - 

1. You enter some input ![You-entering-your-fav-website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6yfxezrsm3r09u78h5s.png)

2. Browser parses the input to check if its a url or a search query. 
For search query => it redirects to the search engine.

3. For URL, it - 
![What-does-browser-do-when-it-encounters-a-url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ej749eq2w80pzskgklvw.png)
💡 In the n/w response, we can see vast differences between 14kb and 15kbs response size, but not between 15 and 16 kbs. Knowing which all Kbs are more critical than others can give you a big time [TTFB](https://web.dev/ttfb/) benefit. More details [here](https://developer.mozilla.org/en-US/docs/Web/Performance/How_browsers_work#tcp_slow_start_14kb_rule).


4. Once the response is fetched, it - 
![what-does-browser-do-after-the-response-is-fetched](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vnv048jlysogeaqj7cc.png)

5. Once renderer process receives the IPC and data stream, it -
  * Starts parsing the HTML received. HTML received is in bytes format.
![HTML Parsing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nb38050dz7qkitlex8v8.png)
  * [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) (the data structure browsers use to process html into web pages) - it contains all the nodes of HTML and their relationships. 
  * When parser comes across downloading resources(like css, js, assets like fonts, image, video), it sends requests to the network thread in the browser process. 
  * Preload scanner is a browser optimisation technique to speeden up this process. It peaks at the tokens generated by parsers and sends requests to the network thread in the browser process.
  * CSS, JS are render blocking resources. They stop html parsing and block page rendering, thus creating more delays and worsening the page load times.  
  * 💡 This is why we use techniques like [preloads](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload), [preconnects](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preconnect), [async](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async), [defer](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer), splitting CSS into critical and non critical and defer the non critical one etc wherever suitable.

6. Once the DOM is constructed, the main thread parses the CSS to create [CSSOM](https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model) - the data structure similar to DOM, but containing the computed styles for each node in the DOM. 
![CSSOM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iu7q5buqlpnvekrdhoy4.png)
7. Now the browser creates an [accessibility tree](https://developer.mozilla.org/en-US/docs/Glossary/Accessibility_tree) - AOM - semantic version of DOM - to be used by screen readers and other accessibility devices.
![Accessibility Object model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7ru7a2abyeg2iswqbrz.png)
8. Now to render the page, the browser needs to know the exact positions of the nodes. So, it creates the render/layout tree, containing the coordinates information for the nodes which are to be shown on the page, considering all line breaks, height and width of elements.
![Render tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrulmy52tlkq3ev9kt51.png)
🤔 Why do we need it, if we have dom and cssom? 
 Because CSS is mighty 💪🏻 It can hide elements from the web page even if they are present in DOM. Vice versa, it can add info(using the pseudo classes), even if they are not present in DOM. It can make the elements float to one side, mask overflow items, and change writing directions.
  - Subsequent calculation of these positions is called reflow.
  - 💡 If for elements like image, we can provide dimensions before loading, reflow can be saved improving the [CLS](https://web.dev/cls/).
9. Next it creates paint records - to add z-index and determine orders in which to paint.
10. Now comes the step where we actually see something on the screen - rasterization - transforming the images created into actual pixels using GPUs. 
To optimise this, the browser creates layers of the page (based on video, canvas, 3d transform, [will-change css property](https://developer.mozilla.org/en-US/docs/Web/CSS/will-change)). These layers are individually rasterized and then composited to form the actual page. 
![Rasterization and Composition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qety6be5bxggs3vo0c2f.png)
  - The first occurrence of this marks the [FCP](https://developer.mozilla.org/en-US/docs/Glossary/First_contentful_paint#:~:text=First%20Contentful%20Paint%20(FCP)%20is,the%20page%20is%20actually%20loading).
  - Also, the entire time duration from when the DNS lookup started to here, marks our [TTI](https://developer.mozilla.org/en-US/docs/Glossary/Time_to_interactive).
  - 💡 When we encounter janky scrolls and animations, we should check reflows and repaints, it must be skipping some frames. 
11. It is now that the renderer process sends a message back to the browser process to replace the spinner with reload icon. And all onload events are fired.


![Its done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgqj6d1e70uw4ou2qzb4.png)



This is how eventful and expensive a web page’s journey is. So, be mindful of the renders next time you code an application.