Dynamically generates new static URLs to invalidate browsers' cache
Project description
- Version:
- 0.1.0
By default, the URI of static resources does not change between deployment and it is up to each project to come with a solution to invalidate the browser’s cache. django-static-url adds helper functions that can be used in Django settings file, in views and templates. The idea is to generate a new /static/ URL each time the dev server is reloaded and a new URL each time the code is deployed in production.
How does it work?
In its simplest form, django-static-url will compute a hash based on the current time and insert this hash between the static url prefix (e.g., /static/) and the static file path. The URL will be recomputed every time the devserver is reloaded so any changes to static files should be picked up by the browser without having to empty or bypass the cache.
In production, you probably do not want the URL to change every time a process is reloaded so you can provide the path of a file whose access time will be used to compute the hash. For example, we give the path of our uwsgi config file because it is accessed only once per deployment.
More strategies will be provided in the future to accommodate various scenarios (e.g., load balanced app servers that do not share files).
django-static-url assumes that you know how to configure your production web server. Presumably, you are using nginx and have added a location block to bypass the python web server to serve your static files. We provide an example location block for nginx in the installation instructions below.
Requirements
django-static-url works with Python 3.4+. It requires Django 1.8+
Installation
Install the library
pip install django-static-url
Add this snippet in your Django Settings (development)
# The prefix of the static URL. STATIC_ROOT_URL = "/static/" # Will change the URL everytime the settings/server is reloaded. from django_static_url_helper import url_helper STATIC_URL = url_helper.get_static_url_now( STATIC_ROOT_URL, True, SECRET_KEY)
Alternative 2: Add this snippet in your Django Settings (production)
# The prefix of the static URL. STATIC_ROOT_URL = "/static/" # Will change the URL everything the SOME_IMPORTANT_FILE_PATH is touched # (url generated based on access time). from django_static_url_helper import url_helper STATIC_URL = url_helper.get_static_url_file( STATIC_ROOT_URL, SOME_IMPORTANT_FILE_PATH, True, SECRET_KEY)
Add this to your urls file (development)
from django.conf import settings from django_static_url_helper.django_url_helper import staticfiles_dynamicurlpatterns # Define your urlpatterns here urlpatterns = ... if settings.DEBUG: urlpatterns += staticfiles_dynamicurlpatterns(settings.STATIC_ROOT_URL)
Add this to your nginx config file (production)
# regex that captures all requests made to # /static(/HASH_CODE)/STATIC_FILE_PATH location ~ ^/static/([a-f0-9-]+/)?(.*)$ { # Set static file expiration expires 7d; alias /path/to/djangoproject/static/$2; }
License
This software is licensed under the New BSD License. See the LICENSE file in the repository for the full license text.
Signing GPG Key
The following GPG keys can be used to sign tags and release files:
Barthelemy Dagenais: 76320A1B901510C4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-static-url-0.1.0.tar.gz
.
File metadata
- Download URL: django-static-url-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85b2cadea0a3469f7ba4915bb483c9f44b70729d74573fa4c762b841c3fe056f |
|
MD5 | a8906e46406a73dbf44150a5edfbdf95 |
|
BLAKE2b-256 | 96eb074d1247af57d64699b0dc1fbeb8b6db705bfe48caabb51a4d7bb400ed8b |
File details
Details for the file django_static_url-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_static_url-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 871a424d9b5517643277daecb100a213b9fd645e762badcee5b63aacf0be8ee9 |
|
MD5 | 4582d3499b1fba5334db7395c996c993 |
|
BLAKE2b-256 | 618e0622e4967d0064af4276c89e7036ad4808ab58fe7149e6ad6ef7ffb7e941 |