Skip to content

Instantly share code, notes, and snippets.

@simonhaenisch
Last active May 21, 2018 02:53
Show Gist options
  • Save simonhaenisch/be44e97f89a4075b0d734cc0240ae6c1 to your computer and use it in GitHub Desktop.
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
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