Skip to content

Instantly share code, notes, and snippets.

@gui-poa gui-poa/gist:8ba1315befeafc915189 Secret
Last active Mar 15, 2017

What would you like to do?
CSS Critical Path

I'm reading a lot about CSS in the Critical Path and I decided to make some tests. I noted there isn't one version that works in all browsers. Only Chrome fails in a version.

I had to make two different versions.


CSS inlined:

#teste { data uri and background color...}
#teste2 { text-indent: -99999px }


{..some data uri to make the css large...}
body #teste2 { font-size: 18px; color: red;text-indent:0; }

The div #teste should be painted as soon as possible and #teste2 will only be visible after the download/parse of waiting2.css

We have to test the way of CSS loading.

  1. Works in Chrome and Safari
<link rel="stylesheet" type="text/css" href="" media="print" id="pacoquinha">

But, at the bottom:

document.getElementById('pacoquinha').media = 'screen';

If I dont put this, the CSS isn't parsed, only downloaded.

To see it again, use the clear button then force reload.

The version that works in Chrome, doesn't work in Firefox/IE11/Android browser.

These versions works in FF, IE11,Android Browser and Safari, but Chrome.

var path   = "css";
var style   = document.createElement( 'link' );
style.rel   = 'stylesheet';
style.type  = 'text/css';
style.href  = "";
document.getElementsByTagName( 'head' )[0].appendChild( style );


<link rel="stylesheet" type="text/css" href="">

</body> JS css link


This comment has been minimized.

Copy link

commented Apr 4, 2016

The Chrome version still blocks the rendering process from performing it's first layout (easy to spot when enabling network throttling). The performance audit report doesn't complain though about blocking CSS (I assume it only does static code analysis due to which it thinks it's all fine). So you do get a higher score, but in the end it does not make a difference for the user. I guess you'll have to load the CSS through XHR and add the response text in an inline <style>


This comment has been minimized.

Copy link

commented Mar 15, 2017

@iLaurens If the browser supports Fetch (most of them will in a few months), the Fetch API can be used in lieu of XHR for inlining CSS:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.