烧瓶没有看到 .js 文件中的变化 [英] flask does not see change in .js file

查看:9
本文介绍了烧瓶没有看到 .js 文件中的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我使用的其中一个 .js 文件进行了更改,无论我做什么,flask 都坚持从内存缓存中获取文件的最新版本,而没有改变.

I made a change on one of the .js files that I use and no matter what I do, flask insists on picking up, from memory cache, the last version of the file, without the change.

为了澄清,我有以下结构.一切从 foo.html

To clarify, I have the following structure. It all starts with foo.html

return render_template foo.html

foo.html 里面有一个表单,它用一些数据调用烧瓶,然后返回第二个模板 bar.html:

foo.html has a form inside that calls flask with some data and then returns a second template bar.html:

return render_template bar.html

第二个模板调用了一些 .js 文件,放置在 static 文件夹中,但它不会在代码更改时更新.

This second template calls some .js file, placed in the static folder, but it doesn't update when the code changes.

我提到上面的结构是因为如果 .js 文件放在 foo.html 而不是 bar.html 上,那么 Flask 选取文件上的新更改.但是在 bar.html 中,Flask 完全忽略了它们.

I mention the structure above because if the .js file was placed on foo.html instead of bar.html then Flask would pick up the new changes on the file. But in bar.html Flask completely ignores them.

发生了什么?

唯一有效的是点击浏览器上的禁用缓存"并重新加载.

The only thing that worked was to click on "disable cache" on the browser and reload again.

推荐答案

最终这是一个令人沮丧的浏览器缓存问题,可以通过强制浏览器进行硬刷新"来解决,这将是一个浏览器/依赖于操作系统的击键,但通常这有效:

Ultimately this is a frustrating browser cache issue, which can be solved by forcing the browser to do a "hard refresh", which is going to be a browser/OS dependent keystroke, but generally this works:

  • Windows:Ctrl+F5
  • Mac:Cmd+Shift+R
  • Linux:Ctrl+Shift+R

还有其他文件名技巧可以用来避免这个问题(在 OP 的评论中提到).这些在您无法控制浏览器行为的生产环境中尤为重要.

There are other filename tricks one can use to avoid this issue (mentioned in comments of the OP). These are especially important in production where you have no control over browser behavior.

对于非静态 Flask 响应,您可以设置 cache_control.max_age 属性,如果它被缓存,它应该告诉浏览器何时使响应过期.例如,如果您有一个返回 JSON 数据的 Flask XHR 端点,您可以这样做:

For non-Static Flask responses you can set the cache_control.max_age property, which should tell the browser when to expire the response if it is cached. For instance if you have a Flask XHR endpoint that returns JSON data you could do this:

@app.route('/_get_ajax_data/')
def get_ajax_data():
    data = {"hello": "world"}
    response = jsonify(data)
    response.cache_control.max_age = 60 * 60 * 24  # 1 day (in seconds)
    return response

您通常还可以在生产网络服务器配置中为特定资源类型(例如 CSS/JS/HTML/JSON/等)设置默认值

You typically can also set default values in your production web server configuration for specific resource types (e.g. CSS/JS/HTML/JSON/etc)

编辑 4/1/2019(与愚人节无关)

  • Mac/Safari 击键现在显示为:Cmd+Opt+R(通过评论,谢谢!).
  • 查看@MarredCheese 的新答案,了解一个非常优雅的文件名技巧",强制浏览器忽略更新文件的缓存副本.

这篇关于烧瓶没有看到 .js 文件中的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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