diff --git a/nyaa/routes.py b/nyaa/routes.py index 419320e..f2398d5 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -46,6 +46,30 @@ def redirect_url(): return '/' return url +_static_cache = {} +@app.template_global() +def static_cachebuster(static_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__) + # But that'd mean every static resource would get cache busted. This lets unchanged items + # stay in the cache. + + if app.debug: + # Do not bust cache on debug (helps debugging) + return static_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 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] @app.template_global() def modify_query(**new_values): diff --git a/nyaa/templates/layout.html b/nyaa/templates/layout.html index 659bb46..fd000ce 100755 --- a/nyaa/templates/layout.html +++ b/nyaa/templates/layout.html @@ -17,26 +17,29 @@ set the column breakpoint to tablet mode, instead of mobile. This is to 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') %} + + - + - + - - + +