Created
December 1, 2015 04:03
-
-
Save mmerickel/61023515229b6645e80d to your computer and use it in GitHub Desktop.
serving cache busted assets without renaming files on disk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_static_regex = re.compile( | |
r''' | |
(?P<root>/static/[a-zA-Z0-9._/-]+) | |
- | |
(?P<buster>[a-fA-F0-9]+) | |
(?P<ext>\.[a-zA-Z0-9]+) | |
$''', | |
re.VERBOSE, | |
) | |
def RemoveCacheBustTokenNotFoundViewFactory(wrapped): | |
def wrapper(context, request): | |
m = _static_regex.match(request.path_info) | |
if m is not None and request.method in ('GET', 'OPTIONS', 'HEAD'): | |
path = m.group('root') + m.group('ext') | |
subreq = request.blank( | |
path, | |
base_url=request.application_url, | |
) | |
try: | |
response = request.invoke_subrequest(subreq, use_tweens=False) | |
response.cache_max_age = 10 * 365 * 34 * 3600 | |
return response | |
except Exception: | |
pass | |
return wrapped(context, request) | |
return wrapper | |
@notfound_view_config( | |
decorator=[ | |
RemoveCacheBustTokenNotFoundViewFactory, | |
], | |
) | |
def notfound_view(exc, request): | |
return exc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment