Skip to content

Instantly share code, notes, and snippets.

@jhned
Last active April 20, 2020 18:27
Show Gist options
  • Save jhned/cb90cd83c99624df7a1818c1865bdb00 to your computer and use it in GitHub Desktop.
Save jhned/cb90cd83c99624df7a1818c1865bdb00 to your computer and use it in GitHub Desktop.
A fallback for stylesheets loaded with prefetch
var DOMTokenListSupports = function (tokenList, token) {
if (!tokenList || !tokenList.supports) {
return;
}
try {
return tokenList.supports(token);
} catch (e) {
if (e instanceof TypeError) {
console.log("The DOMTokenList doesn't have a supported tokens list");
} else {
console.error("That shouldn't have happened");
}
}
};
// test if prefetch is supported
const linkSupportsPreFetch = DOMTokenListSupports(document.createElement("link").relList, "prefetch");
if (!linkSupportsPreFetch) {
const prefetchLinks = document.querySelectorAll('link[rel=prefetch]');
for (i = 0; i < prefetchLinks.length; i++) {
let prefetchLink = prefetchLinks[i];
let linkType = prefetchLink.getAttribute('as');
let linkOnload = prefetchLink.getAttribute('onload');
if ('style' === linkType) {
prefetchLink.setAttribute('rel', 'stylesheet');
prefetchLink.removeAttribute('as');
}
if ('' !== linkOnload) {
prefetchLink.removeAttribute('onload');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment