我使用Django来提供静态文件(dev,localhost )。我希望这些文件被浏览器缓存,但它们不是。因此,我添加了中间件来更改/ Turn off caching of static files in Django development server / urls的响应头(它们修复了相反的问题,但也更改了头)。
问题是文件仍未被缓存。我相信这是因为“异:曲奇”标题。我该如何摆脱这个标题?它似乎是在执行我的中间件StaticCache之后添加的。
浏览器中静态文件的响应头:
Cache-Control:public, max-age=315360000
Content-Length:319397
Content-Type:application/javascript
Date:Mon, 22 Aug 2016 13:34:04 GMT
Expires:Sun, 17-Jan-2038 19:14:07 GMT
Last-Modified:Fri, 19 Aug 2016 23:18:48 GMT
Server:WSGIServer/0.1 Python/2.7.10
Vary:Accept-Encoding, Cookiesettings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'<app>.middleware.BeforeViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'<app>.middleware.StaticCache',
'<app>.middleware.ExceptionMiddleware',
)middleware.py
class StaticCache(object):
def process_response(self, request, response):
if request.path.startswith('/static/'):
response['Cache-Control'] = 'public, max-age=315360000'
response['Vary'] = 'Accept-Encoding'
response['Expires'] = 'Sun, 17-Jan-2038 19:14:07 GMT'
return responseurls.py
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),发布于 2016-08-22 14:04:32
我相信Vary: Cookie头是由SessionMiddleware设置的。由于响应中间件以相反的顺序运行,所以您应该将StaticCache中间件放在会话中间件之前,放在MIDDLEWARE_CLASSES设置中。
https://stackoverflow.com/questions/39081330
复制相似问题