Last active
May 21, 2018 02:53
-
-
Save simonhaenisch/be44e97f89a4075b0d734cc0240ae6c1 to your computer and use it in GitHub Desktop.
Load typekit fonts using the fetch API and abort the request if it takes to long. Example: https://equippers.de
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
if (window.fetch) { | |
(async d => { | |
const controller = new AbortController(); | |
const signal = controller.signal; | |
setTimeout(() => controller.abort(), 750); | |
const res = await fetch('https://use.typekit.net/xxxxxxx.css', { signal }); | |
const css = await res.text(); | |
const style = d.createElement('style'); | |
style.textContent = css; | |
d.querySelector('head > *:last-child').after(style); | |
d.documentElement.classList.add('wf-active'); | |
})(document).catch(() => console.warn('Font loading aborted due to slow network.')); | |
} else { | |
console.error('Your browser sucks. No nice fonts for you.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment