Skip to content

Instantly share code, notes, and snippets.

@BryanYang
Created January 23, 2019 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryanYang/c53b9911ac9b331aadbc7334a14a6d79 to your computer and use it in GitHub Desktop.
Save BryanYang/c53b9911ac9b331aadbc7334a14a6d79 to your computer and use it in GitHub Desktop.
动态prefetch下个页面的资源,提升用户首次体验。
requestIdleCallback(() => {
fetch("xxx/a.html").then(res => res.text()).then(html => {
const reg = /target*.js/gi;
const scripts = html.match(reg);
if(scripts && scripts.length) {
scripts.forEach(src => {
const link = document.createElement("link");
link.rel = "prefetch";
link.href = src;
document.head.appendChild(link);
});
}
})
}, { timeout: 1000 * 5 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment