As a software developer that mainly works with web applications, there's nothing I find more frustrating than working on a new feature or fixing a bug and not seeing my changes reflected in the web browser because it is using an out-of-date cached version of the JS or CSS files I updated. And even after realizing that I may have already fixed the problem and refreshing the browser, it's not an option to explain to puzzled customers that doing a full-page refresh or emptying the browser cache will fix the problem. At HumanGeo, we've developed a technique to get around this problem by using Nginx to dynamically "smart cache" some of our static files by using the built-in URL regular expressions to control what files are served and cached by browsers. We were already using Nginx to serve our static content, so with just a couple of updates to the Nginx configuration and the Django code, we're guaranteed to always get the most up-to-date version of specific static files and ensuring that the browser will properly cache the file.
Here's an example from one of our hosted CSS files:
After doing a basic get request (Note: The requested file was taken from the cache):
After doing a full-page refresh (Note: The requested file got the appropriate status code 304 from the server):
The technique is actually pretty straightforward: using the URL regular expression format that Nginx provides, we look for a pattern within the given URL and find the corresponding file on the file system. So essentially, the browser will request /static/js/application.<pattern>.js and Nginx will actually serve /static/js/application.js. Since the browser is requesting a filename with a particular pattern, it will still cache files with that pattern, but we have much more control over the user experience and what files they see when they hit the page, without requiring them to clear their cache. To further automate the process, the pattern we use is the MD5 hash of the specified file. Using the MD5 and a little help from a Django context processor, we can ensure that the pattern is always up-to-date and that we're always using the most recent version of the file. However, the pattern could be any unique string you'd like - such as a file version number, application version number, the date the file was last modified, etc.
Nginx configuration - how to use the URL regex:
Getting the MD5 for specific files: Django settings.py
Generate and save the MD5 hash for application.js:
Ensure that a custom template context processor has been added to TEMPLATE_CONTEXT_PROCESSORS:
Make the JS_MD5 variable visible to the templates: template_context_processors.py
Reference the JS_MD5 variable in html templates:
When the HTML templates are loaded, the script tag will now be generated as:
With a couple of updates to Django and a few lines of Nginx configuration, it's easy to create dynamic smart caching of static resources that will work across all browsers. This will also ensure that project stakeholders and all of the application's users will also have the correct version of the static resource, as well as their browsers correctly caching each resource.