Preload a Resource - WebPageTest

WebPageTestsnippet

It allows experimenting with preloading certain resources. It suffers from the limitation that JavaScript created preload link tag is loaded a little late as link tag isn't discovered by preloader scanner. Actually adding <link rel=preload might have slighly different results.

console.log('preloadResource script')
try {
    var perfScripts = window.perfScripts = window.perfScripts || {};
    perfScripts.preloadResource = function(config) {
      if(!document.head) {
            console.log('preloadResource: Head not found. Retrying.')
            setTimeout(function() {perfScripts.preloadResource(config)},0)
            return;
      }
        const link = document.createElement('link');
        link.rel="preload"
        link.as=config.as
        link.href=config.href
        console.log('preloadResource:', config);
        document.head.prepend(link)
    }
    //perfScripts.preloadResource({href:"URL_OF_AN_IMAGE", as: "image"});
} catch(e) {
    console.log('preloadResource script gave error', e);
}
... Loading comments