Skip to content

Instantly share code, notes, and snippets.

Created December 7, 2015 11:54
What would you like to do?
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
} else {
// Load non-blocking by default, font-observer will handle class switching later on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment