DChase
DChase

Reputation: 1

(Django, Nginx) Prevent client browsers from caching static files?

My website uses Django, Gunicorn, and Ngnix. I'm trying to get client browsers that access my website to always get the latest static files without having to press Ctrl+F5 to clear the browser cache to get the new static files.

I've found in many places answers similar to the following:

  1. Modify file /etc/ngnix/sites-enabled.
  2. $Sudo Nano mysite.
  3. Add the following code the the server block.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires -1;
}

The only thing that ever happens is either the static files don't load at all or they do load but but the browser is still caching them and they need to be manually cleared by ctrl+F5.

How can I get the browsers to fetch new static files each time?

Upvotes: 0

Views: 1629

Answers (1)

sun_jara
sun_jara

Reputation: 1815

Try the following nginx configuration to instruct browsers not to cache static files.

location / {
  add_header Cache-Control "no-store, no-cache, private";
}

As per HTTP1.1 Specifications

no-store

The purpose of the no-store directive is to prevent the inadvertent release or retention of sensitive information (for example, on backup tapes). The no-store directive applies to the entire message, and MAY be sent either in a response or in a request. If sent in a request, a cache MUST NOT store any part of either this request or any response to it. If sent in a response, a cache MUST NOT store any part of either this response or the request that elicited it. This directive applies to both non- shared and shared caches. "MUST NOT store" in this context means that the cache MUST NOT intentionally store the information in non-volatile storage, and MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible after forwarding it. Even when this directive is associated with a response, users might explicitly store such a response outside of the caching system (e.g., with a "Save As" dialog). History buffers MAY store such responses as part of their normal operation.

The purpose of this directive is to meet the stated requirements of certain users and service authors who are concerned about accidental releases of information via unanticipated accesses to cache data structures. While the use of this directive might improve privacy in some cases, we caution that it is NOT in any way a reliable or sufficient mechanism for ensuring privacy. In particular, malicious or compromised caches might not recognize or obey this directive, and communications networks might be vulnerable to eavesdropping.


However, I would suggest using ManifestStaticFilesStorage instead of not caching static files on browsers. This will append an md5 hash of file content to the file name. Hence every time the file content is changed, the file name changes and the browser will make a new request to the file. In this way, the browser can cache the static files for a longer duration, increasing the page load performance.

To enable the ManifestStaticFilesStorage you have to make sure the following requirements are met:

  • the STATICFILES_STORAGE setting is set to django.contrib.staticfiles.storage.ManifestStaticFilesStorage
  • the DEBUG setting is set to False
  • you’ve collected all your static files by using the collectstatic management command

i.e.

# in settings.py
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
DEBUG = False

Run

python manage.py collectstatic

My references:

Upvotes: 6

Related Questions