Post

CSS is render- but not parser-blocking. This means that while a browser is fetching CSS, it is permitted to continue parsing the rest of your HTML but not render it. HOWEVER! If you’re not careful, you can inadvertently end up in parser-blocking territory.

Take this demo HTML file. A stylesheet and 10,000 paragraphs. Here we can see that the browser is permitted to parse the HTML while the CSS is in flight. This is A Good Thing™.

They are interleaved with rendering tasks, which shows is the browser’s ability to progressively render HTML, but one space character held up the main thread completely!

And why did this happen? Defensively, browsers will not run any synchronous JS if any synchronous CSS is still in flight. This is because the JS could read/write from/to the CSSOM, so the browser will build the CSSOM first, just in case.

0 reposts7 likes

Statistically, this isn’t behaviour you actually need or want very often, so placing JS after CSS often leads to main thread inefficiencies that you could probably circumvent.

Because this stylesheet is defined before some JS, for that entire 6 seconds, their main thread was unable to do any work (other than some receive data events). Totally blocked by CSS. That goes some way toward explaining this: