<script>
(function () {
const getScriptsUrl = () => {
let scriptHtml = document.head.querySelector('noscript').innerHTML.match(/"([^>]*)"/g)
scriptHtml = scriptHtml.map(js => js.replace(/\"/g, ""))
return scriptHtml
}
const requestScript = (scriptUrl) => {
const httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200 || httpRequest.status === 304) {
const assetsScript = document.createElement('script')
assetsScript.defer = true
assetsScript.innerHTML = httpRequest.responseText
document.head.appendChild(assetsScript)
window.localStorage.setItem(scriptUrl + '_' + window.PEAPP.assetsVersion, encodeURIComponent(httpRequest.responseText))
}
}
httpRequest.open('GET', window.PEAPP.assetsPath + scriptUrl + '?_v=' + window.PEAPP.assetsVersion, true)
httpRequest.send(null)
}
const renderScript = () => {
const loadScripts = getScriptsUrl()
for (let i = 0; i < loadScripts.length; i++) {
if (!window.localStorage.getItem(loadScripts[i] + '_' + window.PEAPP.assetsVersion)) {
requestScript(loadScripts[i])
} else {
const assetsScript = document.createElement('script')
assetsScript.defer = true
assetsScript.innerHTML = decodeURIComponent(window.localStorage.getItem(loadScripts[i] + '_' + window.PEAPP.assetsVersion))
setTimeout(() => {
document.head.appendChild(assetsScript)
}, 0)
}
}
}
return {
init() {
renderScript()
}
}
})().init()
</script>
js文件缓存加载
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 合作开发接口整合界面时,界面可以显示出来,可是js,css等样式文件显示不出来,我的解决办法: 在applicat...
- 项目中加载图片用的YYKit的组件YYWebImage,加载完的图片会在内存中存留和缓存到磁盘。但是想要根据图片u...