From d80fed7941c769641899d79a6540436e95ba89e0 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Tue, 13 Feb 2018 23:49:00 +0100 Subject: [PATCH] Force browser to reload changed static resources Add a last modified query parameter to static resource. This will force the browser to re-load resources (css, js...) after a new deploy. --- app/main/views.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/main/views.py b/app/main/views.py index 7cdb4d6..b98736b 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -9,7 +9,8 @@ This module implements the main blueprint. :license: BSD 2-Clause, see LICENSE for more details. """ -from flask import Blueprint, render_template, jsonify +import os +from flask import Blueprint, render_template, jsonify, request, current_app from flask_login import login_required from .. import utils @@ -34,6 +35,24 @@ def handle_csentry_error(error): return response +@bp.app_url_defaults +def modified_static_file(endpoint, values): + """Add a last modified query parameter to static resource + + Force the browser to invalidate the cache and reload resources + when files have changed + + Inspired from http://flask.pocoo.org/snippets/40/ and + https://gist.github.com/Ostrovski/f16779933ceee3a9d181 + """ + if endpoint == 'static': + filename = values.get('filename') + if filename: + # The same static folder is used for all blueprints + file_path = os.path.join(current_app.static_folder, filename) + values['m'] = int(os.stat(file_path).st_mtime) + + @bp.route('/') @login_required def index(): -- GitLab