Skip to content

Instantly share code, notes, and snippets.

@rupl
Created December 7, 2015 11:54
Show Gist options
  • Save rupl/72d66718623a38f9f053 to your computer and use it in GitHub Desktop.
Save rupl/72d66718623a38f9f053 to your computer and use it in GitHub Desktop.
Use Service Worker to load fonts async/direct via client-side logic.
// Load fonts async by default
// Load sync from SW cache when available.
//
// Snippet assumes your Service Worker caches fonts as part of installation, so that
// an 'activated' Service Worker means the fonts are cached with 100% confidence.
if (
'serviceWorker' in navigator &&
navigator.serviceWorker.controller !== null &&
navigator.serviceWorker.controller.state === 'activated'
) {
// Directly load fonts from cache. File contains inlined fonts.
document.write('<link rel="stylesheet" href="/css/fonts.min.css" type="text/css">');
// Skip the font-observer
document.documentElement.classList.remove('wf-loading');
document.documentElement.classList.add('wf-oswald');
} else {
// Load non-blocking by default, font-observer will handle class switching later on.
loadCSS('/css/fonts.min.css');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment