Forked from adactio/Twig-critical-CSS-test.twig
Last active
February 2, 2017 22:13
-
-
Save gellerby/589f228a0e1b474d763f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $cssupdate = '20150318'; ?> | |
<?php if ( isset($_COOKIE[ 'csscached']) && $_COOKIE[ 'csscached'] == $cssupdate ) : ?> | |
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css?v=<?php echo $cssupdate; ?>"> | |
<?php else : ?> | |
<style> | |
<?php include( bloginfo('template_directory') . '/css/critical.css'); ?> | |
</style> | |
<script> | |
/* Load critical CSS and cache stylesheet */ | |
(function(win, doc) { | |
'use strict'; | |
function loadCSS(href) { | |
var ss = doc.createElement('link'), | |
ref = doc.getElementsByTagName('script')[0], | |
sheets = doc.styleSheets; | |
ss.rel = 'stylesheet'; | |
ss.href = href; | |
ss.media = 'only x'; | |
ref.parentNode.insertBefore(ss, ref); | |
function toggleMedia() { | |
var defined, | |
i; | |
for (i = 0; i < sheets.length; i = i + 1) { | |
if (sheets[i].href && sheets[i].href.indexOf(ss.href) > -1) { | |
defined = true; | |
} | |
} | |
if (defined) { | |
ss.media = 'all'; | |
} else { | |
win.setTimeout(toggleMedia); | |
} | |
} | |
toggleMedia(); | |
return ss; | |
} | |
loadCSS(<?php bloginfo('template_directory'); ?> + '?v=<?php echo $cssupdate; ?>'); | |
doc.cookie = 'csscached=<?php echo $cssupdate; ?>;expires="Tue, 19 Jan 2038 03:14:07 GMT";path=/'; | |
}(this, this.document)); | |
</script> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork of https://gist.github.com/adactio/8a15bcdfef855bca16ed updated for Wordpress templates with query strings for cache busting opposed to the Apache rewrite