Last active
April 20, 2020 18:27
-
-
Save jhned/cb90cd83c99624df7a1818c1865bdb00 to your computer and use it in GitHub Desktop.
A fallback for stylesheets loaded with prefetch
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
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