Flask:在静态文件上设置标题

zxz*_*zxz 6 python flask

我有以下烧瓶路线,它提供静态内容:

@app.route('/static/<path:path>')
@resourceDecorator
def getStaticFile(path):
    return send_from_directory('static', path)
Run Code Online (Sandbox Code Playgroud)

@resourceDecorator 声明如下:

def resourceDecorator(f):
    def new_func(*args, **kwargs):
        resp = make_response(f(*args, **kwargs))

        resp.cache_control.no_cache = True                   # Turn off caching
        resp.headers['Access-Control-Allow-Origin'] = '*'    # Add header to allow CORS

        return resp
    return update_wrapper(new_func, f)
Run Code Online (Sandbox Code Playgroud)

装饰器设置标头以停用缓存并允许跨域访问.这适用于我的其他"常规"路由,但通过静态路由发送的文件似乎没有设置其标头.

这里出了什么问题?

sim*_*cci 1

对于静态文件,flask 将默认缓存超时设置为 12 小时/43200 秒,因此您的问题。send_from_directory您可以通过直接传递值来更改默认缓存超时,cache_timeout因为它使用该send_file函数将文件发送到客户端。

send_from_directory(cache_timeout=0)
Run Code Online (Sandbox Code Playgroud)

或者,您可以覆盖get_send_file_max_age.