I’m curious as to what your setup looks like… Do you use https://github.com/typekit/webfontloader to load your fonts? If you do, your fonts should already be cached on subsequent visits. There are callbacks you can hook into to apply custom classes to your html.
In my setup, I’m also using “critical css” for above the fold styles which I’m inlining and then loading the actual stylesheet asynchronously. This does indeed help with very fast initial rendering of the page.
My setup looks like this:
in the head to load my fonts from google for all pages except the home page
-
Inlining above the fold styles on the home page (only)
-
Load fonts through a seperate stylesheet which is loading asynchronously (there’s a small “loadCSS” function in the head
-
The seperate fonts.css stylesheet looks something like this: (it’s what google returns from the above link)
@font-face {
font-family: ‘Open Sans’;
font-style: normal;
font-weight: 300;
src: local(‘Open Sans Light’), local(‘OpenSans-Light’), url(https://fonts.gstatic.com/s/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTQ7aC6SjiAOpAWOKfJDfVRY.woff2) format(‘woff2’);
unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
}
In my home page template it looks something like this:
<head>
...
<?php if ($page->isHomePage()): ?>
<style><?php echo file_get_contents($site->url() . '/assets/styles/critical.min.css') ?></style>
<script>(function(u){function loadCSS(e,t,n){"use strict";function r(){for(var t,i=0;i<d.length;i++)d[i].href&&d[i].href.indexOf(e)>-1&&(t=!0);t?o.media=n||"all":setTimeout(r)}var o=window.document.createElement("link"),i=t||window.document.getElementsByTagName("script")[0],d=window.document.styleSheets;return o.rel="stylesheet",o.href=e,o.media="only x",i.parentNode.insertBefore(o,i),r(),o}for(var i in u){loadCSS(u[i]);}}(['assets/styles/fonts.css','assets/styles/styles.min.css']));</script>
<noscript>
<link href="assets/styles/fonts.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="assets/styles/styles.min.css">
</noscript>
<?php else: ?>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,600' rel='stylesheet' type='text/css'>
<?php echo css('assets/styles/styles.min.css') ?>
<?php endif; ?>
</head>
I’ve used this tool to extract my critical css: https://github.com/addyosmani/critical
It did need some custom editing but that was no big deal for me (removed some media queries, etc)…
Caveats:
On inital rendering of the page the font(s) may still be loading asynchronously, so whenever the font has finished loading, the text on the page that relies on the font may be flickering/refreshing for a brief moment to reflect the new font. I don’t mind this personally.
Here is an interesting article in case you haven`t read it: https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/webfont-optimization
Let me know what you think or if you have any other suggestions / remarks. 