I am trying to understand when exactly the HTML is rendered on screen in the browser.
I read this S.O. answer and tried some use cases and I observed something that doesn't fit into the model shared in the link,
also I can't seem to find a consistent mind-model for the observation.
What is the browser rendering order that fits the cases discussed?
- When HTML parser reaches M1 , M1 is displayed.
- Then parser reaches script tag it wait for js file to download and parsed.
- Then parser reaches M3, M3 is displayed.
Case A is in-coherence with the order described on most answers. Huray!!! Let's move on.
- HTML parser reaches M1, M1 is not displayed as in CASE A
- HTML parser reaches link tag, it wait for css to download and parser.
- M1 and M2 are display, so it waited for stylesheet to download before rendering M1.
- Then parser reaches script tag it wait for js file to download and parsed.
- Then parser reaches M3, M3 is displayed.
So Case-B shows that it didn't render the M1, it waited for CSS to download before rendering it. So maybe the renderer needs to know the CSS before rendering.
Case:C Image C
So from Case B, we assumed that may be render-er needs to know CSS.
Let's look at Case C:
- HTML parser reaches M1, M1 is displayed. It should not have been displayed since as we saw in case B it should have to wait for css to load.
- Now parser reaches script, it waits for js to download and parser.
- M2, M3 are displayed
Edit: Proposed mind-model which explains the above behavior..(but require review/refinement)
Script
is not a renderer blocking taglink
is a renderer blocking tag- Considering that the renderer and HTML parser are two threads.
- HTML parser can send stuff to renderer for rendering.
- HTML parser can send block signal to renderer..to block renderer to render any html which hasnt been rendered atleast once.
- HTML parser can send unblock signal to renderer..to unblock renderer from render any html which hasnt been rendered once.
With above model we can explain CASE B and CASE C:
CASE B Explanation:
- HTML parser reaches M1, M1 is send to renderer.
- HTML parser reaches Link tag, parser send a blocking signal to renderer.
- Renderer before it can render M1 , it got a blocking signal and hence M1 is not display.
- HTML parser complete link tag parsing(downloading) and send a unblocking signal to renderer, upon receiving a unblocking signal it renders the M1.
- HTML parser reaches M2, M2 is send to renderer.
- HTML parser reaches Script tag, since script tag is not a renderer blocking tag, renderer is free to render html.
- HTML parser completes parsing of script tag (downloading).
- HTML parser reaches M3, M3 is send to renderer.
Similarly we can dry run CASE C, and it fits perfectly in above model.
Is my model correct or something wrong with it ?