简体   繁体   English

自动清除浏览器缓存

[英]Auto clear cache from browser

Is there any workaround to auto clear the cache as we update the javascript code?当我们更新 javascript 代码时,是否有任何解决方法可以自动清除缓存? It always happen where when I do the script code changes, from other user device, they won't be able to see the new update of the website because their browser is still reading the old script and they will see this as a bug since they do not know about this cache stuff.当我从其他用户设备更改脚本代码时,总是会发生这种情况,他们将无法看到网站的新更新,因为他们的浏览器仍在读取旧脚本,他们会将此视为错误,因为他们不知道这个缓存的东西。 I have to mention the cache every time when there is an update made which is giving a bad experience to the user.每次进行更新时,我都必须提到缓存,这给用户带来了糟糕的体验。

Usually for an image, I will set some additional unique id from the get method inside the url so that the website is able to read the latest update.通常对于图像,我会在 url 中的 get 方法中设置一些额外的唯一 ID,以便网站能够读取最新更新。 But for javascript / jquery, I wonder if there is any kind of code to force the browser to read the new update instead of manually clearing the cache.但是对于javascript / jquery,我想知道有没有什么代码可以强制浏览器读取新的更新而不是手动清除缓存。

You have 3 options:您有 3 个选择:

  1. Put this in the head section of your html page:把它放在 html 页面的头部:
<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" />

This will make your app to not cache anything at all ( at least you can keep it during development)这将使您的应用程序完全不缓存任何内容(至少您可以在开发过程中保留它)

  1. Use hard refresh for example ctrl+f5 if you are using chrome every time you reload.如果每次重新加载时都使用 chrome,请使用硬刷新,例如 ctrl+f5。

  2. Use a Cache killer plugin for your browser.为您的浏览器使用缓存杀手插件。 There's a bunch of them out there, you just install them and enable them whenever you want.那里有很多,您只需安装它们并在需要时启用它们。

You can add a dummy unique id for you javascript. (eg, /javascript.js?id=123)您可以为您添加一个虚拟唯一 ID javascript。(例如,/javascript.js?id=123)

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

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