简体   繁体   English

使用jquery / Angularjs清除浏览器缓存

[英]clear browser cache using jquery/Angularjs

I'm tying to clear the browser cache programmatically. 我想以编程方式清除浏览器缓存。 I am doing this because i have deployed a new version of the application, but the browser gets old version and contents from cache. 我这样做是因为我已经部署了该应用程序的新版本,但是浏览器从缓存中获取了旧版本和内容。 but i want to clear cache when browser gets new version and load the latest app from server. 但是我想在浏览器获取新版本并从服务器加载最新应用程序时清除缓存。

I have also use location.reload(true); 我也使用location.reload(true); but it is not clearing cache just reload the current page. 但是它并不会清除缓存,只是重新加载当前页面。

You can clear browser cache by using version trick 您可以使用版本技巧清除浏览器缓存

For example, 例如,

<script src="custom.js?version=1.1.1"></script>

just add dummy version at the end of the file type.It will reload the code 只需在文件类型的末尾添加虚拟版本,它将重新加载代码

You can't really clear the cache using Angular or JQuery. 您真的不能使用Angular或JQuery清除缓存。 One way is to force your browser to request fresh resources. 一种方法是强制浏览器请求新资源。

One way to do this is using gulp-rev. 一种方法是使用gulp-rev。

https://github.com/sindresorhus/gulp-rev https://github.com/sindresorhus/gulp-rev

You can't do it from JavaScript. 无法通过JavaScript做到这一点。 What you can do is following: 您可以执行以下操作:

Append some variable string to the end of your content url. 在内容网址的末尾附加一些可变字符串。 Example: 例:

//example.com/script.js?version=1.0

or 要么

var timestamp = new Date().getTime()
var url = "//example.com/script.js?ts=" + timestamp

This essentially fools the browser that the url you are trying to load is a new one and it skips looking for it in the cache. 这从根本上愚弄了浏览器,您尝试加载的URL是一个新的URL,并且跳过了在缓存中查找它的过程。

Timestamp method does have a disadvantage that it doesn't make use of browser caching ever. 时间戳方法确实有一个缺点,就是它永远不会利用浏览器缓存。

Also, make sure that your HTML file has proper content header set so that the html itself is not cached. 另外,请确保您的HTML文件具有正确的内容标头设置,以便不缓存html本身。 Checkout How to control web page caching, across all browsers? 结帐如何在所有浏览器中控制网页缓存? for details on cache control headers. 有关缓存控制标头的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM