小弟在開發練習中遇到一個問題
在點選瀏覽器上一頁時原本的資料會被cache不會再次呼叫後端
已經有在瀏覽器加上
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setDateHeader("Expires", -1);
%>
or
meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"
meta http-equiv="Pragma" content="no-cache"
meta http-equiv="Expires" content="0"
但還是無法解決返回上一頁會出現 from disk cache 的問題,請問有其它解決方法不要讓瀏覽器cache嗎?
javascript 加一行讓上一頁的動作中斷
window.onbeforeunload = function() { return false; };
或
<script type="text/javascript">
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
或
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
<script>
if(performance.navigation.type == 2){
console.log("Doing reload");
location.reload(true);
console.log("Done with reload");
}
console.log("Script loaded.")
</script>
比較強硬的方式:
window.onpageshow = function(event) {
if (event.persisted || (window.performance &&
window.performance.navigation.type == 2)) {
console.log("返回上一頁");
window.location.reload()
}
};
event.persisted
是否自快取讀取(IOS有些會無效)
window.performance.navigation.type
0 = 點擊連結、輸入url等方式
1 = 使用reload
2 = 透過歷史紀錄(上一頁、下一頁)
255 = 其他