Created
December 7, 2015 11:54
-
-
Save rupl/72d66718623a38f9f053 to your computer and use it in GitHub Desktop.
Use Service Worker to load fonts async/direct via client-side logic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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