Update common_settings.py for precompressing static files #2411 #3898
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2411
STATICFILES_STORAGE:
We specify whitenoise.storage.CompressedManifestStaticFilesStorage as the storage backend. This storage class automatically compresses static files during the collectstatic process, creating .gz (gzip) and .br (Brotli) versions of each file.
WHITENOISE_USE_BROTLI:
This setting enables Brotli compression for static files. Brotli typically achieves better compression ratios compared to gzip, so this helps with performance.
COMPRESS_PRECOMPRESS_GZIP and COMPRESS_PRECOMPRESS_BROTLI:
These ensure that both .gz (gzip) and .br (Brotli) compressed versions of static files are created during the collectstatic process.
Serving Precompressed Files:
WhiteNoise will automatically serve precompressed versions of static files if the client supports the respective compression formats (i.e., Brotli or gzip). This reduces the load on the webserver (Caddy) since it doesn’t need to compress responses dynamically.
Caching with WHITENOISE_MAX_AGE:
Static files are served with a cache-control header that allows browsers to cache them for one year, reducing the load on your server and improving performance.
By using WhiteNoise in this manner, we ensure that static files are compressed during the deployment process (e.g., with collectstatic) and served efficiently with support for both Brotli and gzip without requiring the webserver (Caddy) to compress them on the fly.
Technical details
Screenshots
Checklist
Update index.md
).develop
branch of the repositoryvisible errors.
Developer Certificate of Origin
Developer Certificate of Origin