Created
April 14, 2020 20:16
-
-
Save neilstuartcraig/da9e102f355ce029aa8a3a73cd96bc06 to your computer and use it in GitHub Desktop.
WebPageTest preconnect/preload/... script
This file contains hidden or 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
// Stolen from: https://andydavies.me/blog/2019/08/07/experimenting-with-link-rel-equals-preconnect-using-custom-script-injection-in-webpagetest/ | |
(function () { | |
var entries = [ | |
{'href': 'https://nav.files.bbci.co.uk', 'rel': 'preconnect'}, | |
{'href': 'https://nav.files.bbci.co.uk', 'rel': 'preconnect', 'crossOrigin': 'anonymous'} | |
]; | |
var fragment = document.createDocumentFragment(); | |
for(entry of entries) { | |
var link = document.createElement('link'); | |
link.rel = entry.rel; | |
link.href = entry.href; | |
if(entry.hasOwnProperty('crossOrigin')) { | |
link.crossOrigin = entry.crossOrigin; | |
} | |
if(entry.hasOwnProperty('as')) { | |
link.as = entry.as; | |
} | |
fragment.appendChild(link); | |
} | |
document.head.appendChild(fragment); | |
performance.mark('wpt.injectEnd'); // Not essential | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment