Skip to content

Instantly share code, notes, and snippets.

@cvan
Created October 8, 2019 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/8e8f1adc2112cb4f03039f0c656de7c4 to your computer and use it in GitHub Desktop.
Save cvan/8e8f1adc2112cb4f03039f0c656de7c4 to your computer and use it in GitHub Desktop.
loading fonts with JavaScript using `new FontFace` (to avoid FOUF)
(async function () {
async function loadFonts () {
if (!('FontFace' in window)) {
return;
}
const fonts = [
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Light.woff2)'), // 400
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Regular.woff2)'), // 500
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Medium.woff2)'), // 600
];
Promise.all(fonts).forEach(fontsLoaded => {
fontsLoaded.forEach(font => {
await font.load();
document.fonts.add(font);
});
document.body.classList.add('fonts-loaded');
});
}
await loadFonts();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment