{"id":16417342,"url":"https://github.com/adamchainz/django-minify-html","last_synced_at":"2025-12-29T23:42:49.854Z","repository":{"id":36959496,"uuid":"442423588","full_name":"adamchainz/django-minify-html","owner":"adamchainz","description":"Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django.","archived":false,"fork":false,"pushed_at":"2024-10-29T10:48:15.000Z","size":431,"stargazers_count":111,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-29T12:39:55.824Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adamchainz.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://adamj.eu/books/"]}},"created_at":"2021-12-28T10:09:35.000Z","updated_at":"2024-10-29T10:48:18.000Z","dependencies_parsed_at":"2023-12-29T00:24:41.851Z","dependency_job_id":"2dbfedbc-64c8-47c7-9d41-a14d961787d1","html_url":"https://github.com/adamchainz/django-minify-html","commit_stats":{"total_commits":129,"total_committers":4,"mean_commits":32.25,"dds":0.2713178294573644,"last_synced_commit":"236c8f691ee844b1253b8996d0129178f696acc1"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fdjango-minify-html","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fdjango-minify-html/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fdjango-minify-html/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fdjango-minify-html/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adamchainz","download_url":"https://codeload.github.com/adamchainz/django-minify-html/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238394323,"owners_count":19464583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T07:11:27.138Z","updated_at":"2025-12-29T23:42:49.848Z","avatar_url":"https://github.com/adamchainz.png","language":"Python","funding_links":["https://adamj.eu/books/"],"categories":["Python"],"sub_categories":[],"readme":"==================\ndjango-minify-html\n==================\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/django-minify-html/main.yml.svg?branch=main\u0026style=for-the-badge\n   :target: https://github.com/adamchainz/django-minify-html/actions?workflow=CI\n\n.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge\n  :target: https://github.com/adamchainz/django-minify-html/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/django-minify-html.svg?style=for-the-badge\n   :target: https://pypi.org/project/django-minify-html/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\u0026logoColor=white\u0026style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nUse `minify-html \u003chttps://github.com/wilsonzlin/minify-html\u003e`__, the extremely fast HTML + JS + CSS minifier, with Django.\n\n----\n\n**Work smarter and faster** with my book `Boost Your Django DX \u003chttps://adamchainz.gumroad.com/l/byddx\u003e`__ which covers many ways to improve your development experience.\n\n----\n\nRequirements\n------------\n\nPython 3.10 to 3.14 supported.\n\nDjango 4.2 to 6.0 supported.\n\nInstallation\n------------\n\n1. Install with **pip**:\n\n   .. code-block:: sh\n\n       python -m pip install django-minify-html\n\n2. Add django-minify-html to your ``INSTALLED_APPS``:\n\n   .. code-block:: python\n\n       INSTALLED_APPS = [\n           ...,\n           \"django_minify_html\",\n           ...,\n       ]\n\n3. Add the middleware:\n\n   .. code-block:: python\n\n       MIDDLEWARE = [\n           ...,\n           \"django_minify_html.middleware.MinifyHtmlMiddleware\",\n           ...,\n       ]\n\n   The middleware should be *below* any other middleware that may encode your responses, such as Django’s |GZipMiddleware|__.\n   It should be *above* any that may modify your HTML, such as those of `django-debug-toolbar \u003chttps://django-debug-toolbar.readthedocs.io/\u003e`__ or `django-browser-reload \u003chttps://pypi.org/project/django-browser-reload/\u003e`__.\n\n   .. |GZipMiddleware| replace:: ``GZipMiddleware``\n   __ https://docs.djangoproject.com/en/stable/ref/middleware/#django.middleware.gzip.GZipMiddleware\n\nReference\n---------\n\nFor information about what minify-html does, refer to `its documentation \u003chttps://github.com/wilsonzlin/minify-html\u003e`__.\n\n``django_minify_html.middleware.MinifyHtmlMiddleware``\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe middleware runs ``minify_html.minify()`` on the content of HTML responses.\nThis function minifies HTML, and any inline JavaScript and CSS.\n\nThe middleware passes keyword arguments to ``minify()`` from its ``minify_args`` attribute, a dictionary of names to values.\nThese correspond to the values in the Rust library’s `Cfg structure \u003chttps://docs.rs/minify-html/latest/minify_html/struct.Cfg.html\u003e`__, which have defaults in the Python library as visible `in the source \u003chttps://github.com/wilsonzlin/minify-html/blob/master/minify-html-python/src/lib.rs\u003e`__.\nBy default the middleware overrides ``minify_css`` and ``minify_js`` to ``True``.\nIf you need to change an argument, subclass the middleware, replace ``minify_args``, and use your subclass.\nFor example, to preserve comments after minification:\n\n.. code-block:: python\n\n    from django_minify_html.middleware import MinifyHtmlMiddleware\n\n\n    class ProjectMinifyHtmlMiddleware(MinifyHtmlMiddleware):\n        minify_args = MinifyHtmlMiddleware.minify_args | {\n            \"keep_comments\": True,\n        }\n\n(This example uses `the dictionary merge operator \u003chttps://docs.python.org/3.9/whatsnew/3.9.html#dictionary-merge-update-operators\u003e`__.)\n\nThe middleware applies to all non-streaming, non-encoded HTML responses.\nYou can skip it on individual views with the ``@no_html_minification`` decorator, documented below.\n\nTo restrict it more broadly, you can use a subclass with an overridden ``should_minify()`` method.\nThis method accepts the ``request`` and ``response``, and returns a ``bool``.\nFor example, to avoid minification of URL’s with the URL prefix ``/admin/``:\n\n.. code-block:: python\n\n    from django.http import HttpRequest, HttpResponse\n\n    from django_minify_html.middleware import MinifyHtmlMiddleware\n\n\n    class ProjectMinifyHtmlMiddleware(MinifyHtmlMiddleware):\n        def should_minify(self, request: HttpRequest, response: HttpResponse) -\u003e bool:\n            return super().should_minify(request, response) and not request.path.startswith(\n                \"/admin/\"\n            )\n\nNote that responses are minified even when ``DEBUG`` is ``True``.\nThis is recommended because HTML minification can reveal bugs in your templates, so it’s best to always work with your HTML as it will appear in production.\nMinified HTML is hard to read with “View Source” - it’s best to rely on the inspector in your browser’s developer tools.\n\n``django_minify_html.decorators.no_html_minification``\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nApply this decorator to views for which you want to skip HTML minification.\n\n.. code-block:: python\n\n    from django.shortcuts import render\n\n    from django_minify_html.decorators import no_html_minification\n\n\n    @no_html_minification\n    def example_view(request):\n        return render(request, \"problematic-template.html\")\n\nMotivation\n----------\n\nHTML minification is an underappreciated technique for web optimization.\nIt can yield significant savings, even on top of other tools like HTTP compression (try `django-http-compression \u003chttps://pypi.org/project/django-http-compression/\u003e`__ for that.)\n\nThere is an existing package for HTML minification in Django, `django-htmlmin \u003chttps://pypi.org/project/django-htmlmin/\u003e`__.\nBut it is much slower, since it does the minification in Python.\nAt time of writing, it is also unmaintained, with no release since March 2019.\n\nThere are other minifiers out there, but in benchmarks `minify-html \u003chttps://github.com/wilsonzlin/minify-html\u003e`__ surpasses them all.\nIt’s a really well optimized and tested Rust library, and seems to be the best available HTML minifier.\n\nHistorically, Cloudflare provided automatic minification (`removed August 2024 \u003chttps://community.cloudflare.com/t/deprecating-auto-minify/655677\u003e`__).\nThis was convenient at the CDN layer, since it requires no application changes.\nBut it adds some overhead: non-minified HTML has to first be transferred to the CDN, and the CDN has to parse the response, and recombine it.\nIt also means that you don’t get to see the potential side effects of minification until your code is live.\nOverall it should be faster and more predictable to minify within Django, at the point of HTML generation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamchainz%2Fdjango-minify-html","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadamchainz%2Fdjango-minify-html","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamchainz%2Fdjango-minify-html/lists"}