From 18dd7cd66fd908ec372bdd4489f5400b032e94c5 Mon Sep 17 00:00:00 2001 From: shm0o Date: Thu, 25 May 2017 15:01:54 -0400 Subject: [PATCH 01/30] static_cachebuster: build the url with url_for --- nyaa/routes.py | 17 ++++++++--------- nyaa/templates/layout.html | 12 ++++++------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/nyaa/routes.py b/nyaa/routes.py index 8893567..b7ff9fe 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -52,7 +52,7 @@ def redirect_url(): @app.template_global() -def static_cachebuster(static_filename): +def static_cachebuster(filename): ''' Adds a ?t= cachebuster to the given path, if the file exists. Results are cached in memory and persist until app restart! ''' # Instead of timestamps, we could use commit hashes (we already load it in __init__) @@ -61,19 +61,18 @@ def static_cachebuster(static_filename): if app.debug: # Do not bust cache on debug (helps debugging) - return static_filename + return flask.url_for('static', filename=filename) # Get file mtime if not already cached. - if static_filename not in _static_cache: - file_path = os.path.join(app.config['BASE_DIR'], 'nyaa', static_filename[1:]) + if filename not in _static_cache: + file_path = os.path.join(app.static_folder, filename) + file_mtime = None if os.path.exists(file_path): file_mtime = int(os.path.getmtime(file_path)) - _static_cache[static_filename] = static_filename + '?t=' + str(file_mtime) - else: - # Throw a warning? - _static_cache[static_filename] = static_filename - return _static_cache[static_filename] + _static_cache[filename] = file_mtime + + return flask.url_for('static', filename=filename, t=_static_cache[filename]) @app.template_global() diff --git a/nyaa/templates/layout.html b/nyaa/templates/layout.html index e0184be..906289b 100755 --- a/nyaa/templates/layout.html +++ b/nyaa/templates/layout.html @@ -25,10 +25,10 @@ make the navbar not look awful on tablets. --> {# These are extracted here for the dark mode toggle #} - {% set bootstrap_light = static_cachebuster('/static/css/bootstrap.min.css') %} - {% set bootstrap_dark = static_cachebuster('/static/css/bootstrap-dark.min.css') %} + {% set bootstrap_light = static_cachebuster('css/bootstrap.min.css') %} + {% set bootstrap_dark = static_cachebuster('css/bootstrap-dark.min.css') %} - + - + - - + +