Flask: set header for static files

I have the following flask route that serves static content:

@app.route('/static/<path:path>')
@resourceDecorator
def getStaticFile(path):
    return send_from_directory('static', path)

@resourceDecorator declared as follows:

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)

The decorator sets headers to deactivate caching and allows access to the cross domain. This works for my other, “regular” routes, but files sent via a static route do not seem to set their headers.

What is wrong here?

+4
source share
1 answer

- 12 /43200 , , . - send_from_directory, cache_timeout , send_file .

send_from_directory(cache_timeout=0)

get_send_file_max_age.

0

Source: https://habr.com/ru/post/1613785/


All Articles