Heroku + S3 + Django: Static Files not Cached

2019-07-26 22:49发布

Currently have a project deployed on Heroku with static files loaded from S3. I'm using boto/django-storage to manage my S3 content, but if I call the same view or load the same page repeatedly, all the images/static content load twice and is not cached.

I've placed AWS_HEADERS = { 'Cache-Control': 'max-age=2592000', }

in my settings.py, but the reason seems the same exact images (refreshed + loaded twice) have different signatures in their URL? I've tried multiples headers, but the browser doesn't seem want to cache it and instead loads them all everytime.

1条回答
对你真心纯属浪费
2楼-- · 2019-07-26 23:27

try setting AWS_QUERYSTRING_AUTH = False. Then the URL generated will always be the same (public) URL. The default-ACL in S3BotoStorageis public-read, which shouldn't be changed then.

Two things not to forget:

  • perhaps you want to add public, max-age=XXX, so public proxies also can cache your content?
  • When you want the browser to cache that long, you should keep in mind that the filenames have to change when you change the content. One solution would be to S3BotoStorage combined with the Django-CachedStaticFilesStorage (see here, but I use it without the seperate cache-backend)
查看更多
登录 后发表回答