Skip to content

Commit 5ec6d70

Browse files
authored
Improve WhiteNoise configuration (#199)
* Switch from the deprecated `STATICFILES_STORAGE` Django config option to `STORAGES` * Add `whitenoise.runserver_nostatic` to `INSTALLED_APPS` so that WhiteNoise's runserver implementation is used instead of Django's default implementation, for improved dev-prod parity * Enable `WHITENOISE_KEEP_ONLY_HASHED_FILES`, which means the original (un-hashed filename) copy of static assets are no longer unnecessarily stored in the slug. (This does mean all Django templates must use the `static` processor for asset URLs, but all of the templates in this guide do that already.) * Use the `brotli` WhiteNoise package extra, so that the Brotli package ends up being installed, so that static assets can also be served using the more efficient Brotli compression (for browsers that support it, which is all modern browsers), and not just GZip. See: https://whitenoise.readthedocs.io/en/latest/django.html
1 parent 4465fc2 commit 5ec6d70

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

gettingstarted/settings.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
# Application definition
4545

4646
INSTALLED_APPS = [
47+
# Use WhiteNoise's runserver implementation instead of the Django default, for dev-prod parity.
48+
"whitenoise.runserver_nostatic",
4749
# Uncomment this and the entry in `urls.py` if you wish to use the Django admin feature:
4850
# https://docs.djangoproject.com/en/4.2/ref/contrib/admin/
4951
# "django.contrib.admin",
@@ -57,6 +59,10 @@
5759

5860
MIDDLEWARE = [
5961
"django.middleware.security.SecurityMiddleware",
62+
# Django doesn't support serving static assets in a production-ready way, so we use the
63+
# excellent WhiteNoise package to do so instead. The WhiteNoise middleware must be listed
64+
# after Django's `SecurityMiddleware` so that security redirects are still performed.
65+
# See: https://whitenoise.readthedocs.io
6066
"whitenoise.middleware.WhiteNoiseMiddleware",
6167
"django.contrib.sessions.middleware.SessionMiddleware",
6268
"django.middleware.common.CommonMiddleware",
@@ -148,8 +154,17 @@
148154
STATIC_ROOT = BASE_DIR / "staticfiles"
149155
STATIC_URL = "static/"
150156

151-
# Enable WhiteNoise's GZip compression of static assets.
152-
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
157+
STORAGES = {
158+
# Enable WhiteNoise's GZip and Brotli compression of static assets:
159+
# https://whitenoise.readthedocs.io/en/latest/django.html#add-compression-and-caching-support
160+
"staticfiles": {
161+
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
162+
},
163+
}
164+
165+
# Don't store the original (un-hashed filename) version of static files, to reduce slug size:
166+
# https://whitenoise.readthedocs.io/en/latest/django.html#WHITENOISE_KEEP_ONLY_HASHED_FILES
167+
WHITENOISE_KEEP_ONLY_HASHED_FILES = True
153168

154169

155170
# Test Runner Config

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
django>=4.0,<5.0
22
gunicorn>=20.0,<21.0
33
dj-database-url>=2.0,<3.0
4-
whitenoise>=6.0,<7.0
4+
whitenoise[brotli]>=6.0,<7.0
55
psycopg2>=2.0,<3.0

0 commit comments

Comments
 (0)