为什么Google Chrome浏览器不重新加载我的脚本? [英] Why isn't Google Chrome reloading my scripts?

查看:125
本文介绍了为什么Google Chrome浏览器不重新加载我的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个名为"engine"的脚本,经过大量的bashing和(徒劳的)调试后,我发现GC根本没有重新加载它!

So, I have a script called "engine", and after much headbashing and (futile) debugging, I've found out that GC simply isn't reloading it!

这就是我将其包含在网页中(在<head>元素内)的方式:

This is how I include it in the webpage (inside the <head> element):

<script type="text/javascript" src="engine.js"></script>

在脚本开始处放置10个console.log("asdf");时,就好像它们不存在一样.当我转到GC控制台中的资源"选项卡时,我发现对该脚本没有进行任何更改! le?在末尾添加+ "?" + new Date()会有所帮助吗?

When a put 10 console.log("asdf");'s at the start of the script, it's like they aren't there. When I went to the "resources" tab in the GC console, I saw that no changes are being applied whatsoever to that script! Hlep? Would putting a + "?" + new Date() at the end help?

推荐答案

适用于Chrome,Firefox和IE的通用解决方案是通过 Ctrl + Shift 清除缓存+ Del (在Mac上为 + Shift + ).

The universal solution that works in Chrome, Firefox and IE is cleaning the cache via Ctrl+Shift+Del (on Mac +Shift+).

  1. 打开开发人员工具( F12 + + i ,或右键单击→检查).
  2. 选择网络标签,然后选中禁用缓存复选框.
  1. Open Developer Tools (F12 or ++i, or right-click → Inspect).
  2. Select the Network tab and tick the Disable cache checkbox.

  1. 重新加载页面.

❗️注意:打开devtools窗口时,将仅 禁用缓存.

❗️Note: The cache will be disabled only when the devtools window is open.

这仅在不使用#1的情况下才有意义.

  1. 打开开发人员工具.
  2. 点击右下角的设置齿轮图标.
  3. 在出现的对话框中,选择 网络 子部分下的 Disable cache 复选框:从现在开始,当devtools窗口打开.关闭devtools窗口后,缓存将照常工作.
  1. Open Developer Tools.
  2. Click the Settings cogwheel icon in the bottom right corner.
  3. In the dialog that appears, select under the Network subsection the Disable cache checkbox: from now on the cache will be skipped when the devtools window is open. When the devtools windows is closed caching will work as usual.

  1. 打开开发人员工具(否则,下一步中的菜单将不会显示).
  2. 点击并按住 Refresh (刷新)按钮,然后从下拉列表中选择 Empty Cache and Hard Reload .
  1. Open Developer Tools (otherwise the menu in the next step won't show).
  2. Click and hold down the Refresh button, and then select from the dropdown Empty Cache and Hard Reload.

与浏览器无关的解决方案(可用于调试)是在服务器端代码中添加随机生成的版本字符串作为查询参数,即,将脚本称为:

A browser-agnostic solution which could be used for debugging is to append in your server-side code a randomly-generated version string as a query parameter, i.e. call your script as:

<script type="text/javascript" src="myscript.js?ver=12345"></script>

ver参数的值更改时,此技巧将迫使浏览器重新加载脚本.如果您提出ajax请求,则可以将"?ver=" + new Date().getTime()附加到您的URL.

This trick will force the browser to reload the script when the value of the ver parameter changes. If you make ajax requests then you can append "?ver=" + new Date().getTime() to your URL.

注意:完成调试后,请不要忘记删除该参数,因为在生产环境中,您很可能确实希望缓存脚本.不过,通常的做法是在每个新版本上生成一个新时间戳-可以在生产环境中使用它,并确保在新部署后客户端始终获得更新的脚本.

NOTE: Don't forget to remove the parameter when you are finished debugging because in production you most likely do want the scripts to be cached. It is a common practice though to generate a new timestamp on each new build — this can be used in production, and will ensure that after a new deployment the clients will always get the updated scripts.


与上述所有解决方案不同,即使您在客户端和服务器之间进行某种类型的缓存(例如redis,memcached,清漆)或CDN(例如akamai,cloudflare,cloudfront等),该解决方案仍然可以工作.

这篇关于为什么Google Chrome浏览器不重新加载我的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆