.html文件防止script腳本緩存的三種方法
場(chǎng)景
我們有一個(gè)工具庫(kù)通過(guò) script 放在了全局中,它會(huì)提供一個(gè)帶有許多方法的全局對(duì)象 gTool。就像下面這樣:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script type="text/javascript" src="https://xxxx/common/tool.min.js"></script> // [!code hl] </head> <body> <div id="app"><!--app-html--></div> <script type="module" src="/src/main.ts"></script> </body> </html>
然后我們就可以在 main.ts 里能使用 gTool.jumpLink 方法啦。
gTool.jumpLink({})
問(wèn)題
但是現(xiàn)在有這樣一個(gè)問(wèn)題,由于一些原因,我們經(jīng)常需要修改 gTool 的代碼然后上傳更新 cdn。但是用戶通過(guò) html 文件訪問(wèn)的 gTool 鏈接實(shí)際上還是緩存的,根本不會(huì)去請(qǐng)求服務(wù)器,也就導(dǎo)致用戶的 gTool 版本可能過(guò)低 bug 沒(méi)有被修復(fù)。
所以現(xiàn)在問(wèn)題就是如何禁止緩存。
方法
這里我嘗試了 3 個(gè)方法
1 創(chuàng)建 script 標(biāo)簽并 appendChild 到 body
最終我選擇了這種方案,滿足需求并且上線后沒(méi)有問(wèn)題。
可以使用 js 動(dòng)態(tài)創(chuàng)建 script 節(jié)點(diǎn)并插入到 dom 中去。 但是需要注意動(dòng)態(tài)創(chuàng)建的 script 腳本是異步的,不會(huì)阻塞瀏覽器向下執(zhí)行 js。
下面這段代碼,永遠(yuǎn)都是 'body 執(zhí)行' 先于 'tool 腳本執(zhí)行' 打印。這樣就導(dǎo)致獲取不到全局的 gTool 對(duì)象,也就導(dǎo)致報(bào)錯(cuò)了。
<!DOCTYPE html> <html> <head> <script> var htmlScriptScript = document.createElement('script') htmlScriptScript.type = 'text/javascript' htmlScriptScript.src = 'https://xxxx/common/tool.min.js?a=' + Math.random() htmlScriptScript.onload = function () { console.error('tool 腳本執(zhí)行') } document.head.appendChild(htmlScriptScript) </script> </head> <body> <div id="app"><!--app-html--></div> <script> console.error('body 執(zhí)行') </script> <script type="module" src="/src/main.ts"></script> </body> </html>
為了保證 gTool 加載完成后才向下執(zhí)行腳本,我們可以自己寫一個(gè)阻塞函數(shù)。如下:
<!DOCTYPE html> <html> <head> <script> var htmlScriptScript = document.createElement('script') htmlScriptScript.type = 'text/javascript' htmlScriptScript.src = 'https://xxxx/common/tool.min.js?a=' + Math.random() htmlScriptScript.onload = function () { window.gTool = gTool } document.head.appendChild(htmlScriptScript) </script> </head> <body> <div id="app"><!--app-html--></div> <script> + async function main() { + function awaitTool() { + return new Promise(resolve => { + const interval = setInterval(() => { + if (window.gTool) { + clearInterval(interval) + resolve(null) + } + }, 50) + }) + } + await awaitTool(); + // other code + } + main() </script> <script type="module" src="/src/main.ts"></script> </body> </html>
main()
函數(shù)會(huì)每隔 50ms 去檢查 window 上是否有 gTool 對(duì)象,有就說(shuō)明 gTool 腳本已經(jīng)加載完了,可以繼續(xù)往下執(zhí)行了,否則就繼續(xù)遞歸執(zhí)行 main()
,知道 gTool 腳本加載完畢。
2 在地址后加一個(gè)隨機(jī)參數(shù)
并不是很好的解決方案。
這種方法雖然可行,但是每次都需要發(fā)版。因?yàn)榘l(fā)版需要走流程審核,而且有發(fā)版次數(shù)限制。
下面兩種引入方式都是一樣的,只不過(guò)第二種每次重新打包的時(shí)候會(huì)自動(dòng)刷新時(shí)間戳。
<script type="text/javascript" src="https://xxxx/common/tool.min.js?time=1124112341"></script> // [!code hl] <!-- 或者 --> <script type="text/javascript" src="https://xxxx/common/tool.min.js?time=<%=Math.random()%>"></script>
3 document.write()
這種方法不行。
使用 document.write API 來(lái)防止 .html 文件緩存 script 腳本。
在 MDN 里寫了,從 Chrome 55 開始,Chrome(可能)不會(huì)運(yùn)行通過(guò) document.write() 注入的 <script>
,以防止使用 2G 連接的用戶找不到 HTTP 緩存。所以這種方法用來(lái)動(dòng)態(tài)注入 <script>
腳本根本不可行。
<script> document.write("<script src='https://xxxx/common/tool.min.js?rnd=" + Math.random() + "'></s " + " cript> ") </script>
以上就是.html文件防止script腳本緩存的三種方法的詳細(xì)內(nèi)容,更多關(guān)于.html防止script緩存的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ES6學(xué)習(xí)教程之Map的常用方法總結(jié)
Map 是 ES6 中新增的一種數(shù)據(jù)結(jié)構(gòu),與 Set 一起添加,其實(shí)功能都差不多。下面這篇文章主要給大家總結(jié)介紹了關(guān)于ES6學(xué)習(xí)教程之Map的常用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08JavaScript 復(fù)制對(duì)象與Object.assign方法無(wú)法實(shí)現(xiàn)深復(fù)制
這篇文章主要介紹了JavaScript 復(fù)制對(duì)象與Object.assign方法無(wú)法實(shí)現(xiàn)深復(fù)制,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11原生JS實(shí)現(xiàn)動(dòng)態(tài)添加新元素、刪除元素方法
這篇文章主要介紹了原生js實(shí)現(xiàn)動(dòng)態(tài)添加新元素、刪除元素方法 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05js判斷生效時(shí)間不得大于失效時(shí)間的思路及代碼
生效時(shí)間不得大于失效時(shí)間在一些推銷、優(yōu)惠方面還是比較實(shí)用的,接下來(lái)一起看下詳細(xì)的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04JS插件plupload.js實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了PHP結(jié)合plupload.js JS插件實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條加刪除實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11jquery根據(jù)錨點(diǎn)offset值實(shí)現(xiàn)動(dòng)畫切換
點(diǎn)擊后僵硬的切換是不是很不爽,下面為大家介紹的是根據(jù)錨點(diǎn)offset值來(lái)實(shí)現(xiàn)動(dòng)畫切換,喜歡的朋友不要錯(cuò)過(guò)2014-09-09