mirror of
https://github.com/asimonson1125/asimonson1125.github.io.git
synced 2026-02-24 21:09:49 -06:00
static file fingerprinting
used to clear caches when static files are changed
This commit is contained in:
21
src/app.py
21
src/app.py
@@ -1,12 +1,33 @@
|
|||||||
import flask
|
import flask
|
||||||
from flask_minify import Minify
|
from flask_minify import Minify
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import hashlib
|
||||||
import werkzeug.exceptions as HTTPerror
|
import werkzeug.exceptions as HTTPerror
|
||||||
from config import *
|
from config import *
|
||||||
from monitor import monitor, SERVICES
|
from monitor import monitor, SERVICES
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
# Compute content hashes for static file fingerprinting
|
||||||
|
static_file_hashes = {}
|
||||||
|
for dirpath, _, filenames in os.walk(app.static_folder):
|
||||||
|
for filename in filenames:
|
||||||
|
filepath = os.path.join(dirpath, filename)
|
||||||
|
relative = os.path.relpath(filepath, app.static_folder)
|
||||||
|
with open(filepath, 'rb') as f:
|
||||||
|
static_file_hashes[relative] = hashlib.md5(f.read()).hexdigest()[:8]
|
||||||
|
|
||||||
|
@app.context_processor
|
||||||
|
def override_url_for():
|
||||||
|
def versioned_url_for(endpoint, **values):
|
||||||
|
if endpoint == 'static':
|
||||||
|
filename = values.get('filename')
|
||||||
|
if filename and filename in static_file_hashes:
|
||||||
|
values['v'] = static_file_hashes[filename]
|
||||||
|
return flask.url_for(endpoint, **values)
|
||||||
|
return dict(url_for=versioned_url_for)
|
||||||
|
|
||||||
# Add security and caching headers
|
# Add security and caching headers
|
||||||
@app.after_request
|
@app.after_request
|
||||||
def add_security_headers(response):
|
def add_security_headers(response):
|
||||||
|
|||||||
Reference in New Issue
Block a user