Skip to content

Instantly share code, notes, and snippets.

@mtorre4580
Last active March 8, 2023 01:35
Show Gist options
  • Save mtorre4580/71ee3710519f89187d850901eb5971f9 to your computer and use it in GitHub Desktop.
Save mtorre4580/71ee3710519f89187d850901eb5971f9 to your computer and use it in GitHub Desktop.
Prefetch resources example via xhr
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