清除缓存的 JavaScript 包含在 Firefox 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4670738/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:33:42  来源:igfitidea点击:

Clear cached JavaScript includes in Firefox

javascriptfirefoxcaching

提问by user151841

I'm working on JavaScript for a site, developing with Firefox, and when I refresh the page, I don't see my changes. The JavaScript file is in an external file. I reloaded and refreshed the page several times, but the old JavaScript file was still cached. Finally, I loaded the JavaScript page in the browser directly, saw the old script, hit 'reload', and saw my changes.

我正在为一个站点使用 JavaScript,使用 Firefox 进行开发,当我刷新页面时,我看不到我的更改。JavaScript 文件位于外部文件中。我重新加载并刷新了页面几次,但旧的 JavaScript 文件仍然被缓存。最后,我直接在浏览器中加载了 JavaScript 页面,看到了旧脚本,点击“重新加载”,看到了我的更改。

How can I clear cached external JavaScript files? I'll need to know this also when I tell the client that the changes are made, so that they aren't seeing the old cached functionality.

如何清除缓存的外部 JavaScript 文件?当我告诉客户端进行了更改时,我也需要知道这一点,以便他们看不到旧的缓存功能。

采纳答案by armonge

In Firefox you can install a plugin called Web Developer Toolbarwhich has a appcache clearcommand

在 Firefox 中,您可以安装一个名为Web Developer Toolbar的插件,它有一个appcache clear命令

I think there is no way to do it programmatically but you could give a hint to the browser using something like

我认为没有办法以编程方式做到这一点,但你可以使用类似的东西给浏览器一个提示

<script type="text/javascript" src='js/my.js?x=<?php echo rand(0,100) ?>'></script>

回答by Thai

To bypass cache for one time in Firefox:

在 Firefox 中暂时绕过缓存:

  • Click the reload button while holding the shiftkey.
  • Ctrl+F5
  • Ctrl+Shift+Ror Cmd+Shift+R
  • for other browsers
  • 按住键的同时单击重新加载按钮shift
  • Ctrl+F5
  • Ctrl+ Shift+R或 Cmd Shift++R
  • 对于其他浏览器

Some web hosting services docache the page server-side. When bypassing cache, web browsers will send a header to tell the server that it should not respond with the cached data.

一些网络托管服务缓存页面服务器端。绕过缓存时,Web 浏览器将发送一个标头,告诉服务器它不应使用缓存数据进行响应。

回答by Pointy

Browsers have user-facing facilities to clear the cache. Usually it's a menu option somewhere. You can't force the cache to be cleared.

浏览器具有面向用户的工具来清除缓存。通常它是某个地方的菜单选项。您不能强制清除缓存。

What you cando is arrange for your scripts to be loaded from URLs that vary according to version number (or whatever):

可以做的是安排您的脚本从根据版本号(或其他)而变化的 URL 加载:

<script src='http://your.site.com/js/big_script.js?version=2'></script>

Now when you update the code, you update the pages that use it:

现在,当您更新代码时,您会更新使用它的页面:

<script src='http://your.site.com/js/big_script.js?version=3'></script>

That's a different URL, and it won't be in the cache.

这是一个不同的 URL,它不会在缓存中。

回答by jAndy

A very popular technique is to use a querystring parameter. Could look like

一种非常流行的技术是使用查询字符串参数。看起来像

<script src="http://www.somedomain.com/foobar.js?v=1></script>

If you change this line into v=2a browser will reload the script if it was cached before.

如果您将此行更改为v=2浏览器将重新加载之前缓存的脚本。

回答by Andrew

Disable js caches: Dev tools (F12) > Network > Disable cache

禁用 js 缓存:开发工具 (F12) > 网络 > 禁用缓存

Then refresh page: F5

然后刷新页面:F5

回答by Raph Levien

Shift-reload often clears out caches more aggressively. However, you really don't want to rely on this. A good technique is to version the filenames of your external Javascript, and update the HTML that refers to them when you rev. That way, you can rely on caching better as well (for example, setting cache headers to "public" in your webserver, and also specifying long Expires times).

Shift-reload 通常会更积极地清除缓存。但是,您真的不想依赖于此。一个好的技术是对外部 Javascript 的文件名进行版本控制,并在您修改时更新引用它们的 HTML。这样,您也可以更好地依赖缓存(例如,在您的网络服务器中将缓存标头设置为“公共”,并指定较长的过期时间)。