禁用浏览器缓存

发布时间:2020-04-27编辑:佚名阅读(1108)

解决方案:

1.使用js向url添加随机参数(注意:若为hash模式,则随机参数需要放置在 # 前)

但是这样做也有一个弊端,因为缓存可以减少对服务器的直接访问,减少服务器的压力。浏览器不读取缓存里的内容之后,每次都会访问服务器,这样就会增加服务器的压力。所以应根据情况使用。

if (!window.name) {
  location.href += "?random=" + Date.now();
  window.name = "reloaded";
}

2.在html的head中添加meta(浏览器仍希望缓存的话无效)

<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" />

3.后端设置get请求的response请求头

response.setDateHeader("Expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");

4.在浏览器中设置

F12打开控制台--->Network---->Disable cache 打钩

F12--->F1--->network ----> Disable cache(while DevTools is open) 打钩

5.更多工具选项 或 ctrl+shift+delete 或 ctrl+f5----> 清除浏览数据

 总结:

想让浏览器有何种行为,服务端只能通过响应头的方式来设置

想让服务器知道何种行为,浏览器只能通过请求头的方式来设置

  关键字:禁用浏览器缓存


鼓掌

0

正能量

0

0

呵呵

0


评论区