Javascript 停止 Chrome 缓存我的 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8392441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop Chrome Caching My JS Files
提问by Howdy_McGee
I will make a change to my JS files but it won't really change in the browser, I have to rename the files every time so that it reloads it. Is there some sort of .htaccess command I can add or something to make it stop caching?
我将对我的 JS 文件进行更改,但它不会在浏览器中真正更改,我每次都必须重命名文件,以便重新加载它。是否有某种 .htaccess 命令我可以添加或使其停止缓存?
It is even caching my html pages hard core. I need to reopen my entire browser just to see changes. Could it possibly be a server problem?
它甚至缓存我的 html 页面的核心。我需要重新打开整个浏览器才能看到更改。会不会是服务器问题?
回答by MartinHN
You can click the settings icon on top right corner ...
| More Tools | Developer Tools| Network| Disable cache(while DevTools is open)
您可以点击右上角的设置图标...
| 更多工具 | 开发者工具| 网络| 禁用缓存(当 DevTools 打开时)
For windows, this is F12or CTRL + SHIFT + Iwhile on mac CMD + SHIFT + Iopens up DevTools.
对于 Windows,这是F12或CTRL + SHIFT + I同时在 mac 上CMD + SHIFT + I打开 DevTools。
New path for Chrome Update Sept 2018:
2018 年 9 月 Chrome 更新的新路径:
Click settings icon on the top right corner ...
| Settings| Preferences| Developer Tools| Network| Disable cache(while DevTools is open)
点击右上角的设置图标...
| 设置| 偏好| 开发者工具| 网络| 禁用缓存(当 DevTools 打开时)
回答by Nicole Rivers
A faster way to do this is by right clicking the refresh icon beside the address bar and choosing Empty Cache and Hard reload
一种更快的方法是右键单击地址栏旁边的刷新图标,然后选择清空缓存和硬重新加载
Just make sure Chrome's dev tools is open. (Press F12) By the way... This trick only works on Chrome for Windows, Ubuntu, and Mac OS
只需确保 Chrome 的开发工具已打开。(按 F12)顺便说一句......这个技巧只适用于 Windows、Ubuntu 和 Mac OS 的 Chrome
回答by StilesCrisis
Hold shift while clicking the reload button.
单击重新加载按钮时按住 shift。
回答by EGOrecords
add Something like script.js?a=[random Number]
with the Random number generated by PHP.
添加类似script.js?a=[random Number]
PHP 生成的随机数的东西。
Have you tried expire=0, the pragma "no-cache" and "cache-control=NO-CACHE"? (I dunno what they say about Scripts).
您是否尝试过expire=0,编译指示“no-cache”和“cache-control=NO-CACHE”?(我不知道他们怎么说脚本)。
回答by Mike Christensen
A few ideas:
一些想法:
- When you refresh your page in Chrome, do a CTRL+F5 to do a full refresh.
- Even if you set the expires to 0, it will still cache during the session. You'll have to close and re-open your browser again.
- Make sure when you save the files on the server, the timestamps are getting updated. Chrome will first issue a
HEAD
command instead of a full GET to see if it needs to download the full file again, and the server uses the timestamp to see.
- 在 Chrome 中刷新页面时,按 CTRL+F5 进行完全刷新。
- 即使您将 expires 设置为 0,它仍会在会话期间缓存。您必须再次关闭并重新打开浏览器。
- 确保在服务器上保存文件时,时间戳正在更新。Chrome 会先发出
HEAD
命令而不是完整的 GET 来查看是否需要再次下载完整文件,服务器使用时间戳来查看。
If you want to disable caching on your server, you can do something like:
如果要禁用服务器上的缓存,可以执行以下操作:
Header set Expires "Thu, 19 Nov 1981 08:52:00 GM"
Header set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
Header set Pragma "no-cache"
In .htaccess
在.htaccess中
回答by StackG
Quick steps:
快速步骤:
1) Open up the Developer Tools dashboard by going to the Chrome Menu -> Tools -> Developer Tools
1) 转到 Chrome 菜单 -> 工具 -> 开发人员工具,打开开发人员工具仪表板
2) Click on the settings icon on the right hand side (it's a cog!)
2)点击右侧的设置图标(这是一个齿轮!)
3) Check the box "Disable cache (when DevTools is open)"
3) 选中“禁用缓存(DevTools 打开时)”框
4) Now, while the dashboard is up, just hit refresh and JS won't be cached!
4) 现在,当仪表板启动时,只需点击刷新,JS 将不会被缓存!
回答by Ryan Ternier
When doing updates to my web applications I will either use a handler to return a single JS file to avoid the massive hits on my server, or add a small query string to them:
在对我的 Web 应用程序进行更新时,我将使用处理程序返回单个 JS 文件以避免在我的服务器上出现大量点击,或者向它们添加一个小的查询字符串:
<script type="text/javascript" src="/mine/myscript?20111205"></script>
回答by AgilePro
The problem is that Chrome needs to have must-revalidate
in the Cache-Control` header in order to re-check files to see if they need to be re-fetched.
问题是 Chrome 需要must-revalidate
在 Cache-Control` 标头中重新检查文件以查看它们是否需要重新获取。
You can always SHIFT-F5 and force Chrome to refresh, but if you want to fix this problem on the server, include this response header:
您始终可以 SHIFT-F5 并强制 Chrome 刷新,但如果您想在服务器上解决此问题,请包含以下响应标头:
Cache-Control: must-revalidate
This tells Chrome to check with the server, and see if there is a newer file. IF there is a newer file, it will receive it in the response. If not, it will receive a 304 response, and the assurance that the one in the cache is up to date.
这会告诉 Chrome 检查服务器,看看是否有更新的文件。如果有更新的文件,它将在响应中收到它。如果不是,它将收到 304 响应,并保证缓存中的响应是最新的。
If you do NOT set this header, then in the absence of any other setting that invalidates the file, Chrome will nevercheck with the server to see if there is a newer version.
如果您不设置此标头,则在没有任何其他使文件无效的设置的情况下,Chrome 将永远不会检查服务器以查看是否有更新的版本。
Here is a blog postthat discusses the issue further.
这是一篇博客文章,进一步讨论了这个问题。
回答by Ramon Araujo
I know this is an "old" question. But the headaches with caching never are. After heaps of trial and errors, I found that one of our web hosting providers had through CPanel this app called Cachewall. I just clicked on Enable Development mode and... voilà!!! It stopped the long suffered pain straight away :) Hope this helps somebody else... R
我知道这是一个“老”问题。但是缓存的头痛从来都不是。经过大量的试验和错误,我发现我们的一个网络托管服务提供商通过 CPanel 安装了这个名为 Cachewall 的应用程序。我只是点击了启用开发模式,然后......瞧!!!它立即停止了长期遭受的痛苦:)希望这可以帮助其他人...... R
回答by Alfabravo
<Files *>
Header set Cache-Control: "no-cache, private, pre-check=0, post-check=0, max-age=0"
Header set Expires: 0
Header set Pragma: no-cache
</Files>