Skip to content

Commit

Permalink
Fix static files for container images/Debian packages when DEBUG is o…
Browse files Browse the repository at this point in the history
…n (1.15) (#2788)

Co-authored-by: Jan Klopper <[email protected]>
  • Loading branch information
dekkers and underdarknl authored Apr 8, 2024
1 parent 0514e93 commit 2b9d052
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env-defaults
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ DJANGO_CSRF_TRUSTED_ORIGINS=http://localhost,http://127.0.0.1

# This allows running pytest inside the container
ROCKY_DB_USER_CREATEDB=CREATEDB

# This is normally False when DEBUG is true, but we override that in settings.py
# so it possible to set DEBUG to True in production like environments.
COMPRESS_ENABLED=False
1 change: 1 addition & 0 deletions rocky/.ci/.env.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Rocky
SECRET_KEY=change-this
DEBUG=True
COMPRESS_ENABLED=False
TWOFACTOR_ENABLED=False

# Postgresql setup for rocky-db and rocky
Expand Down
5 changes: 3 additions & 2 deletions rocky/rocky/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,13 @@
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"compressor.finders.CompressorFinder",
]
COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", True)
COMPRESS_OFFLINE = True
COMPRESS_STORAGE = "compressor.storage.BrotliCompressorFileStorage"

STORAGES = {
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
"BACKEND": "rocky.storage.RockyStaticFilesStorage",
},
}

Expand All @@ -321,7 +322,7 @@

def immutable_file_test(path, url):
# Match filename with 12 hex digits before the extension e.g.
# app.db8f2edc0c8a.js. Confifguraring this is necessary because whitenoise
# app.db8f2edc0c8a.js. Configuring this is necessary because whitenoise
# doesn't automatically detect the django-compressor files as immutable.
return _IMMUTABLE_FILE_TEST_PATTERN.match(url)

Expand Down
16 changes: 16 additions & 0 deletions rocky/rocky/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.conf import settings
from whitenoise.storage import CompressedManifestStaticFilesStorage


class RockyStaticFilesStorage(CompressedManifestStaticFilesStorage):
def url(self, name, force=False):
if settings.DEBUG and settings.COMPRESS_ENABLED:
# If django-compressor is enabled when DEBUG is also enabled, we
# need to force Django to use the hashed urls so that
# django-compressor can find the static assets in the offline
# manifest. This is needed to make it possible to use DEBUG with
# container images or Debian packages where we only ship the hashed
# files.
force = True

return super().url(name, force)

0 comments on commit 2b9d052

Please sign in to comment.