Skip to content

Instantly share code, notes, and snippets.

@zakirt
Last active September 28, 2015 00:19
Show Gist options
  • Save zakirt/eb9cd215170997e8ad55 to your computer and use it in GitHub Desktop.
Save zakirt/eb9cd215170997e8ad55 to your computer and use it in GitHub Desktop.
Optimize CSS performance with requestAnimationFrame bug fixed.
<!DOCTYPE html>
<html>
<head>
<style>
/*
Inline above the fold CSS
*/
</style>
<noscript><link rel="stylesheet" href="/css/below-the-fold.min.css"></noscript>
</head>
<body>
<!-- HTML Content -->
<script>
/*
Load below the fold CSS
*/
// Use setTimeout to make this JS code asynchronous
setTimeout(function() {
var cb = function() {
var l = document.createElement('link');
var h = document.getElementsByTagName('head')[0];
l.rel = 'stylesheet';
l.href = '/css/below-the-fold.min.css';
h.parentNode.insertBefore(l, h);
};
var vendors = ['ms', 'moz', 'webkit', 'o'];
var raf = window.requestAnimationFrame;
for (var i = 0, len = vendors.length; i < len && !raf; i += 1) {
raf = window[vendors[i] + 'RequestAnimationFrame'];
}
if ( raf ) {
raf(cb);
} else {
window.addEventListener('load', cb);
}
});
</script>
</body>
</html>
@mt3o
Copy link

mt3o commented Sep 21, 2015

Can you use my trick with async css from here: https://gist.github.com/mt3o/4767557b64cdd8c6d6d1 to load nonctritical css?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment