Rom*_*man 1 python caching heroku flask
我正在使用以下代码将静态文件缓存在heroku上托管的flask应用程序中:
# cache control
@app.after_request
def add_header(response):
# rule so it will only affect static files
rule = request.path
if "static" in rule:
response.cache_control.max_age = 1000000
return response
else:
return response
Run Code Online (Sandbox Code Playgroud)
工作正常。
但是,现在我做了一些更改,我需要该站点加载新文件。如果我在已经打开过的常规浏览器中打开该站点,它将加载旧文件(因为它们已被缓存)。
在隐身模式下或按ctrl + f5 =会加载新文件。问题是普通用户不会按ctrl + f5或使用隐身模式。
正如@ mpf82所建议的,您可以简单地向要重新加载的文件添加版本或某些内容作为查询字符串参数。
如果文件名更改,浏览器将不再缓存旧文件。
在flask变量中,未知变量将url_for作为查询字符串处理,因此您只需选择未知变量fe,version然后在其中添加版本号fe 12052017:
<script type=text/javascript src="{{ url_for('static', filename='js/main.js', version='12052017') }}"></script>
Run Code Online (Sandbox Code Playgroud)
就是这样,结果是:
<script type=text/javascript src="/static/js/main.js?version=12052017"></script>