Last active
March 8, 2023 01:35
-
-
Save mtorre4580/71ee3710519f89187d850901eb5971f9 to your computer and use it in GitHub Desktop.
Prefetch resources example via xhr
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 resources = [ | |
'https://unpkg.com/react@18/umd/react.production.min.js', | |
'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js', | |
]; | |
/** Apply prefetch for subsequences */ | |
function prefetchResources(resources) { | |
for (var i = 0; i < resources.length; i++) { | |
var xhrRequest = new XMLHttpRequest(); | |
xhrRequest.open('GET', resources[i], true); | |
xhrRequest.send(); | |
} | |
} | |
/** Validate if the current device has good connection */ | |
function isGoodConection() { | |
return !/\slow-2g|2g|3g/.test(navigator.connection.effectiveType); | |
} | |
if (isGoodConection()) { | |
console.info('Prefetching resources....'); | |
prefetchResources(resources); | |
} else { | |
console.warn('Skip prefetching resources, the user has poor connection'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment