diff --git a/nyaa/routes.py b/nyaa/routes.py index 251ebe5..c4eb33d 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..91bb818 100755 --- a/nyaa/templates/layout.html +++ b/nyaa/templates/layout.html @@ -6,9 +6,9 @@ - - - + + + @@ -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') %} - + - + - - + +