Created
January 23, 2019 06:27
-
-
Save BryanYang/c53b9911ac9b331aadbc7334a14a6d79 to your computer and use it in GitHub Desktop.
动态prefetch下个页面的资源,提升用户首次体验。
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
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