forked from rDrama/rDrama
1
0
Fork 0

Implement hash-based cachebusting module.

Adds the files.helpers.assetcache module, which runs on worker startup
to compute CRC digests of files in given asset directories and provides
a Jinja filter to create cachebusted URLs based on those hashes.
This alleviates the need to increment cachebusting counters in query
strings entirely for files it is applied to.
master
Snakes 2022-09-24 02:40:26 -04:00
parent 319667fea6
commit ac66eb07e9
Signed by: Snakes
GPG Key ID: E745A82778055C7E
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import os
import zlib
from collections import defaultdict
ASSET_DIR = 'files/assets'
ASSET_SUBDIRS = ['/css', '/js']
ASSET_URL = '/assets/'
ASSET_CACHE = defaultdict(lambda: None)
def assetcache_build(asset_dir, subdirs):
for subdir in subdirs:
for root, dirs, files in os.walk(asset_dir + subdir):
for fname in files:
fpath = root + '/' + fname
relpath = fpath[len(asset_dir) + 1:].replace('\\', '/')
with open(fpath, 'rb') as f:
fhash = zlib.crc32(f.read())
ASSET_CACHE[relpath] = '%x' % fhash
def assetcache_hash(asset_path):
return ASSET_CACHE[asset_path]
def assetcache_path(asset_path):
cachehash = assetcache_hash(asset_path)
url = ASSET_URL + asset_path
if cachehash:
url += '?v=' + cachehash
return url
assetcache_build(ASSET_DIR, ASSET_SUBDIRS)

View File

@ -3,6 +3,7 @@ from .get import *
from os import listdir, environ
from .const import *
import time
from files.helpers.assetcache import assetcache_path
@app.template_filter("post_embed")
def post_embed(id, v):
@ -16,6 +17,11 @@ def post_embed(id, v):
return ''
@app.template_filter("asset")
def template_asset(asset_path):
return assetcache_path(asset_path)
@app.template_filter("timestamp")
def timestamp(timestamp):