Django package for minifying and compressing static files
Project description
A modern Django package for minifying and compressing static files during collectstatic with minimal configuration.
Features
CSS/JS Minification: Uses rjsmin and rcssmin for fast minification
Dual Compression: Gzip and Brotli compression support
Django Integration: Seamless integration with Django’s static file system
Selective Processing: Only processes appropriate file types
Hashed Filenames: Maintains Django’s manifest system
Configurable: Fine-grained control over processing options
Installation
Install from PyPI:
pip install django-minify-compress-staticfiles
Configuration
For Django 4.2+, update your STORAGES setting:
STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django_minify_compress_staticfiles.storage.MinicompressStorage",
},
}
For Django < 4.2, use the legacy setting:
STATICFILES_STORAGE = "django_minify_compress_staticfiles.storage.MinicompressStorage"
Settings
All settings use the MINICOMPRESS_ prefix:
- MINICOMPRESS_ENABLED
Enable/disable processing (default: True)
- MINICOMPRESS_MINIFY_FILES
Enable CSS/JS minification (default: True)
- MINICOMPRESS_GZIP_COMPRESSION
Enable Gzip compression (default: True)
- MINICOMPRESS_BROTLI_COMPRESSION
Enable Brotli compression (default: True)
- MINICOMPRESS_MIN_FILE_SIZE
Minimum file size for compression in bytes (default: 200)
- MINICOMPRESS_MAX_FILE_SIZE
Maximum file size for processing in bytes (default: 10485760, i.e., 10MB) Files larger than this are skipped to prevent memory exhaustion. Adjust based on your available memory and security requirements.
- MINICOMPRESS_MAX_FILES_PER_RUN
Maximum number of files to process per collectstatic run (default: 1000) Prevents CPU and memory exhaustion when processing large numbers of files. Increase only if you have verified your system can handle it.
- MINICOMPRESS_COMPRESSION_LEVEL_GZIP
Gzip compression level (default: 6, range: 0-9) Level 6 provides a good balance between compression ratio and CPU usage. Higher values (8-9) consume significantly more CPU with diminishing returns. Lower values (0-5) are faster but produce larger compressed files.
- MINICOMPRESS_COMPRESSION_LEVEL_BROTLI
Brotli compression quality (default: 4, range: 0-11) Level 4 offers excellent compression with reasonable CPU usage. Higher values (8-11) can cause severe CPU spikes during collectstatic. Lower values (0-3) are faster but less effective compression.
- MINICOMPRESS_PRESERVE_COMMENTS
Preserve bang comments in CSS/JS (default: True)
- MINICOMPRESS_SUPPORTED_EXTENSIONS
Dictionary of file extensions to process (default: css, js, txt, xml, json, svg, md, rst, html, htm)
- MINICOMPRESS_EXCLUDE_PATTERNS
List of glob patterns to exclude from processing (default: ["*.min.*", "*-min.*", "*.gz", "*.br", "*.zip"]) Pre-compressed files (e.g., .gz, .br, .zip) are excluded by default to prevent double-compression and security issues.
Usage
Run collectstatic as usual:
python manage.py collectstatic --noinput
The package will automatically:
Minify CSS and JavaScript files
Create .gz and .br compressed versions
Update Django’s manifest with minified file paths
Skip already processed files and patterns
Supported File Types
Minification: CSS, JavaScript
Compression: CSS, JS, TXT, XML, JSON, SVG, MD, RST, HTML, HTM
Files matching *.min.* or *-min.* patterns are excluded from processing.
Security and Performance Considerations
The package implements the following safeguards to mitigate common attack vectors and ensure resource stability.
Path Traversal Protection
To prevent directory traversal attacks (e.g., ../etc/passwd), all file paths undergo strict validation. The system enforces a boundary check ensuring no read or write operations occur outside the defined STATIC_ROOT. Any attempt to access parent directories via relative paths is intercepted and blocked.
Memory Exhaustion Prevention
To prevent memory exhaustion, the MAX_FILE_SIZE setting enforces a hard cap on file processing. This prevents the application from attempting to buffer or process excessively large files that could lead to Out-Of-Memory (OOM) errors.
CPU Exhaustion & Resource Throttling
Compression is a CPU-intensive task. To balance throughput with system stability, the default compression levels are tuned for efficiency:
Gzip: Level 6
Brotli: Level 4
These defaults prevent “CPU pinning” where a single request monopolizes processor cycles.
Compression Bomb Protection
The processor automatically excludes files that are already compressed (e.g., .gz, .br, .zip, .png). This prevents recursive compression cycles and “Zip Bomb” style attacks that could lead to exponential CPU and disk space consumption.
Integrity & Cache Validation
File fingerprinting utilizes SHA-256 hashing. While the primary use case is robust cache invalidation (versioning), SHA-256 was chosen over MD5 or SHA-1 to provide a collision-resistant mechanism that meets modern security compliance standards.
Recommended Settings for Production
For production deployments with high security requirements:
MINICOMPRESS_MAX_FILE_SIZE = 2097152 # 2MB
MINICOMPRESS_MAX_FILES_PER_RUN = 500
MINICOMPRESS_COMPRESSION_LEVEL_GZIP = 6
MINICOMPRESS_COMPRESSION_LEVEL_BROTLI = 4
For development environments with faster builds:
MINICOMPRESS_COMPRESSION_LEVEL_GZIP = 1
MINICOMPRESS_COMPRESSION_LEVEL_BROTLI = 0
MINICOMPRESS_BROTLI_COMPRESSION = False # Disable for faster builds
Dependencies
Required:
Django >= 4.2
Python >= 3.10
brotli >= 1.0.0
rjsmin >= 1.2.0
rcssmin >= 1.1.0
License
BSD 3-Clause License. See LICENSE file for details.
Contributing
Contributions are welcome! Please see the OpenWISP contributing guidelines for more information.
Support
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_minify_compress_staticfiles-1.0.tar.gz.
File metadata
- Download URL: django_minify_compress_staticfiles-1.0.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c64787ea4a2349587181c9af961f036dae2b0278dd014af073252e7aa90f6f90
|
|
| MD5 |
50545b4dc14e9ac16ebb1858aa2068e2
|
|
| BLAKE2b-256 |
ad01dc43c257a918f518cf496622f448d31f409564d3469c8735220ba2cfb7a0
|
Provenance
The following attestation bundles were made for django_minify_compress_staticfiles-1.0.tar.gz:
Publisher:
pypi.yml on openwisp/django-minify-compress-staticfiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_minify_compress_staticfiles-1.0.tar.gz -
Subject digest:
c64787ea4a2349587181c9af961f036dae2b0278dd014af073252e7aa90f6f90 - Sigstore transparency entry: 885963005
- Sigstore integration time:
-
Permalink:
openwisp/django-minify-compress-staticfiles@d5f3df5ab544bb4cd5469560fe18a87c7b944276 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/openwisp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d5f3df5ab544bb4cd5469560fe18a87c7b944276 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl.
File metadata
- Download URL: django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c6abb8d71953fea496757c65b9390ca8c9a092481fcd3d78e98a342a914fcdd
|
|
| MD5 |
6b10e85e5480a0d1e5cbefe14da6f423
|
|
| BLAKE2b-256 |
a7bd41eed3567d3689a43e889e6ef299502474e770aa90a05bfc41e33cde6075
|
Provenance
The following attestation bundles were made for django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl:
Publisher:
pypi.yml on openwisp/django-minify-compress-staticfiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl -
Subject digest:
0c6abb8d71953fea496757c65b9390ca8c9a092481fcd3d78e98a342a914fcdd - Sigstore transparency entry: 885963048
- Sigstore integration time:
-
Permalink:
openwisp/django-minify-compress-staticfiles@d5f3df5ab544bb4cd5469560fe18a87c7b944276 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/openwisp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d5f3df5ab544bb4cd5469560fe18a87c7b944276 -
Trigger Event:
release
-
Statement type: