Skip to main content

Django package for minifying and compressing static files

Project description

CI build status Coverage PyPI Version License

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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_minify_compress_staticfiles-1.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl (12.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_minify_compress_staticfiles-1.0.tar.gz.

File metadata

File hashes

Hashes for django_minify_compress_staticfiles-1.0.tar.gz
Algorithm Hash digest
SHA256 c64787ea4a2349587181c9af961f036dae2b0278dd014af073252e7aa90f6f90
MD5 50545b4dc14e9ac16ebb1858aa2068e2
BLAKE2b-256 ad01dc43c257a918f518cf496622f448d31f409564d3469c8735220ba2cfb7a0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_minify_compress_staticfiles-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0c6abb8d71953fea496757c65b9390ca8c9a092481fcd3d78e98a342a914fcdd
MD5 6b10e85e5480a0d1e5cbefe14da6f423
BLAKE2b-256 a7bd41eed3567d3689a43e889e6ef299502474e770aa90a05bfc41e33cde6075

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page