Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asynchronously loading the webfont + caching it #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions layouts/about.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@
{{> footer }}
</body>
</html>
<link rel="stylesheet" href="/static/css/prism-tomorrow.css" media="all">
20 changes: 20 additions & 0 deletions layouts/partials/footer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@

</footer>

<script type="text/javascript">
(function(d) {
try {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
if(this.responseText) {
addStyle(this.responseText);
window.localStorage.fonts_css_cache = this.responseText;
}
});
xhr.open('get', '/static/fonts/webfont.css');
xhr.send();
} catch(e) {
var c = d.createElement('link'), sc = d.getElementsByTagName("script")[0];
c.href = '//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,600,600italic,700,700italic';
c.rel = 'stylesheet';
sc.parentNode.insertBefore(c,sc);
}
})(document);
</script>
<link rel="stylesheet" href="/static/css/prism-tomorrow.css" media="all">
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js" async defer></script>
<script type="text/javascript">
Expand Down
24 changes: 20 additions & 4 deletions layouts/partials/html-head.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
<meta charset="utf-8">
<title>{{#if title}}{{ title }} | {{/if}}{{ site.title }}</title>

<link rel="dns-prefetch" href="http://fonts.googleapis.com">
<link rel="dns-prefetch" href="http://fonts.gstatic.com">
<link rel="dns-prefetch" href="http://www.google-analytics.com">

<meta name="author" content="{{ site.author }}">
<meta name="robots" content="{{#if robots}}{{ robots }}{{else}}index, follow{{/if}}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -29,4 +25,24 @@
ga('create', 'UA-67020396-1', 'auto');
ga('send', 'pageview');
</script>
<script type="text/javascript">
var addStyle = (function(d) {
return function(style) {
if(!d.querySelector('#fontcache')) {
var c = d.createElement('style'), sc = d.getElementsByTagName("script")[0];
c.innerHTML = style;
c.id = 'fontcache';
sc.parentNode.insertBefore(c,sc);
}
}
})(document);
(function(d,w,s) {
if('querySelector' in d && 'localStorage' in w && s.getItem('fonts_css_cache') !== null) {
addStyle(s.fonts_css_cache);
}
})(document, window, window.localStorage);
</script>
<noscript>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,600,600italic,700,700italic">
</noscript>
</head>