add cachebusting for static files
This commit is contained in:
parent
0ae1fe260b
commit
017aa64c03
4
app.py
4
app.py
|
@ -3,6 +3,7 @@ from flask_sqlalchemy import SQLAlchemy
|
||||||
from sqlalchemy import MetaData
|
from sqlalchemy import MetaData
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
import version
|
import version
|
||||||
|
from lib import cachebust
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -36,9 +37,10 @@ if 'SENTRY_DSN' in app.config:
|
||||||
app.config['SENTRY_CONFIG']['release'] = version.version
|
app.config['SENTRY_CONFIG']['release'] = version.version
|
||||||
sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])
|
sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])
|
||||||
|
|
||||||
|
url_for = cachebust(app)
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_static():
|
def inject_static():
|
||||||
from flask import url_for
|
|
||||||
def static(filename, **kwargs):
|
def static(filename, **kwargs):
|
||||||
return url_for('static', filename=filename, **kwargs)
|
return url_for('static', filename=filename, **kwargs)
|
||||||
return {'st': static}
|
return {'st': static}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from .auth import require_auth
|
from .auth import require_auth
|
||||||
from .interval import decompose_interval
|
from .interval import decompose_interval
|
||||||
from .interval import SCALES as interval_scales
|
from .interval import SCALES as interval_scales
|
||||||
|
from .cachebust import cachebust
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
from flask import url_for, abort
|
||||||
|
import os
|
||||||
|
def cachebust(app):
|
||||||
|
@app.route('/static-<int:timestamp>/<path:filename>')
|
||||||
|
def static_cachebust(timestamp, filename):
|
||||||
|
path = os.path.join(app.static_folder, filename)
|
||||||
|
mtime = os.stat(path).st_mtime
|
||||||
|
if abs(mtime - timestamp) > 1:
|
||||||
|
abort(404)
|
||||||
|
else:
|
||||||
|
resp = app.view_functions['static'](filename=filename)
|
||||||
|
resp.headers.set('cache-control', 'public, immutable, max-age=%s' % (60*60*24*365,))
|
||||||
|
if 'expires' in resp.headers:
|
||||||
|
resp.headers.remove('expires')
|
||||||
|
return resp
|
||||||
|
|
||||||
|
@app.context_processor
|
||||||
|
def replace_url_for():
|
||||||
|
return dict(url_for = cachebust_url_for)
|
||||||
|
|
||||||
|
def cachebust_url_for(endpoint, **kwargs):
|
||||||
|
if endpoint == 'static':
|
||||||
|
endpoint = 'static_cachebust'
|
||||||
|
path = os.path.join(app.static_folder, kwargs.get('filename'))
|
||||||
|
kwargs['timestamp'] = int(os.stat(path).st_mtime)
|
||||||
|
return url_for(endpoint, **kwargs)
|
||||||
|
|
||||||
|
return cachebust_url_for
|
Loading…
Reference in New Issue