How do I add the Cache-Control header to my Heroku hosted static files?
I want to enable browser caching of images, css and java script files.
https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers
The article tells the hero about this in the conclusion:
"Once you understand the concepts of HTTP caching, the next step is to implement them in your application. Most modern web frameworks make this a trivial task."
Can anyone tell me how to accomplish this trivial task? I have a django-python application.
source to share
Static files like images, css and javascript should be served directly by the web server (like Apache nginx), not Django. Therefore, you should configure any caching in your web server configuration, not Django.
If you are hosting a Django project on Heroku, they seem to recommend whitenoise according to this article .
source to share
Your static resources should indeed be served from a web server like nginx or apache, not directly from django, but to answer your question, django uses the application staticfiles
it is supposed to use to manage those resources, if so, use
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.CachedStaticFilesStorage'
in your file settings.py
to enable django caching for static files.
cf https://docs.djangoproject.com/en/1.6/ref/contrib/staticfiles/#cachedstaticfilesstorage https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/#cachedstaticfilesstorage
[link 1.6 explains more about how it works]
For caching in general in django, I recommend reading https://docs.djangoproject.com/en/1.7/topics/cache/
where you can see how to use site level caching middleware or a decorator cache_control
to cache a view like so:
from django.views.decorators.cache import cache_control
@cache_control(must_revalidate=True, max_age=3600)
def my_view(request):
# ...
source to share