Is it possible to preload or otherwise cache @font-face fonts, most likely with javascript, before the page loads so you don\'t get that ugly jump when the page finally does
Use the standard CSS Font Loading API.
Wait for (all) the fonts to load, and then show your content:
document.fonts.ready.then((fontFaceSet) => {
console.log(fontFaceSet.size, 'FontFaces loaded.');
document.getElementById('waitScreen').style.display = 'none';
});
Demo CodePen.
Since 2017 you have preload
MDN: The preload value of the element's rel attribute allows you to write declarative fetch requests in your HTML , specifying resources that your pages will need very soon after loading, which you therefore want to start preloading early in the lifecycle of a page load, before the browser's main rendering machinery kicks in. This ensures that they are made available earlier and are less likely to block the page's first render, leading to performance improvements.
<link rel="preload" href="/fonts/myfont.eot" as="font" crossorigin="anonymous" />
Always check browser compatibility.
It is most useful for font preloading (not waiting for the browser to find it in some CSS). You can also preload some logos, icons and scripts.
A simple technique is to put this somewhere in your index:
<div class="font_preload" style="opacity: 0">
<span style="font-family: 'myfontface#1font-family', Arial, sans-serif;"></span>
<span style="font-family: 'myfontface#2font-family', Arial, sans-serif;"></span>
...
</div>
Tested on Chrome 34, Safari 7 and FF 29 and IE 11
This should solve your problem.
To answer your initial question: yes you can. Only Gecko and WebKit browsers support it currently though.
You just need to add link tags in your head:
<link rel="prefetch" href="pathto/font">
<link rel="prerender" href="pathto/page">
There are a few techniques for "preloading" here: http://paulirish.com/2009/fighting-the-font-face-fout/
Mostly tricking the browser into downloading the file as fast as possible..
You can also deliver it as a data-uri, which helps a lot. and also you could hide the page content and show it when its ready.
Via Google's webfontloader
var fontDownloadCount = 0;
WebFont.load({
custom: {
families: ['fontfamily1', 'fontfamily2']
},
fontinactive: function() {
fontDownloadCount++;
if (fontDownloadCount == 2) {
// all fonts have been loaded and now you can do what you want
}
}
});