[IMP] base: change cache busting middleware

In the HTTP pipeline, there is a middleware called DisableCacheMiddleware
that is called on every request to check if we are in debug mode, to remove
the cache headers and replace them with a static 'no-cache' so the browser
has to contact the server and check for a new version of every ressouce.

There were 2 problems with it:
1. it was doing more work than strictly needed when not in debug, like
rebuilding a list of headers for every request
2. it was removing Etag (case sensitive) with a typo and other headers
that could have been left in place.

This commit fixes those 2 issues by only modifying the headers if we are
in debug mode, and only removing the header 'cache-control' and replacing
it with 'cache-control: no-cache'
This commit is contained in:
Vincent Schippefilt
2019-05-22 07:52:07 +00:00
parent 8a28cc22fd
commit b11b6bc902
+6 -7
View File
@@ -1248,17 +1248,16 @@ class DisableCacheMiddleware(object):
parsed = urls.url_parse(referer)
debug = parsed.query.count('debug') >= 1
new_headers = []
unwanted_keys = ['Last-Modified']
if debug:
new_headers = [('Cache-Control', 'no-cache')]
unwanted_keys += ['Expires', 'Etag', 'Cache-Control']
for k, v in headers:
if k not in unwanted_keys:
new_headers.append((k, v))
for k, v in headers:
if k.lower() != 'cache-control':
new_headers.append((k, v))
start_response(status, new_headers)
start_response(status, new_headers)
else:
start_response(status, headers)
return self.app(environ, start_wrapped)
class Root(object):