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

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

问题描述

我对我使用的 .js 文件中的一个进行了更改,无论我做什么,瓶子都坚持要从内存缓存中取出最后一个版本的文件,没有改变。

为了澄清,我有以下结构。这一切都始于 foo.html

  return render_template foo.html 

foo.html 然后返回第二个模板 bar.html

  return render_template bar.html 


$ b 第二个模板调用一些 .js 文件,放置在静态文件夹中,但是当代码改变时它不会更新。



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



发生什么事了?




解决方案

最终,这是一个令人沮丧的浏览器缓存问题,可以通过强制浏览器做一个硬刷新,这将是一个浏览器/操作系统相关的击键解决,但通常这Works:


  • Windows:Ctrl + F5

  • Mac:Cmd + Shift + R

  • Linux:Ctrl + Shift + R



可以使用其他文件名技巧来避免此问题(在OP的意见中提到)。对于非静态的Flask响应,您可以设置 cache_control.max_age <$ p
$ b

对于非静态的Flask响应, / code>属性,它应该告诉浏览器缓存的响应何时到期。例如,如果你有一个返回JSON数据的Flask XHR端点,你可以这样做:

  @ app.route('/ _ get_ajax_data /')
def get_ajax_data():
data = {hello:world}
response = jsonify(data)
response.cache_control.max_age = 60 * 60 * 24#1天(以秒为单位)
返回响应

您通常也可以设置default您的生产Web服务器配置中的特定资源类型(例如CSS / JS / HTML / JSON等)的值

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.

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

return render_template foo.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

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

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.

What is happening?

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

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.

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

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

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

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