Roman
Roman

Reputation: 3941

Python Flask force reload cached JS files

I am using following code to cache my static files on my flask app which is hosted on heroku:

# 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

It works fine.

But now I made some changes and I need that the site loads the new files. If I open the site in regular browser where I already opened it, it loads the old files (because they are cached).

In incognito mode or hitting ctrl+f5 = loads the new files. The problem is a regular user wont hit ctrl+f5 or use incognito mode.

Upvotes: 2

Views: 4594

Answers (1)

Roman
Roman

Reputation: 3941

As @mpf82 proposed you can simply add a version or something as a query string argument to the file which you want to reload.

If the filename changes the browser wont cache the old file anymore.

In flask variables which are unknown in an url_for are handled as query strings, so you simply choose a variable which is unknown, f.e. versionand add there a version number, f.e. 12052017:

<script type=text/javascript src="{{ url_for('static', filename='js/main.js', version='12052017') }}"></script>

And thats it, the result:

<script type=text/javascript src="/static/js/main.js?version=12052017"></script>

Upvotes: 10

Related Questions