Commit 1839e954 by Timothée Peignier

add html minifier middleware (thanks aaronbassett)

parent dd9d59ee
from django.utils.encoding import DjangoUnicodeDecodeError
from django.utils.html import strip_spaces_between_tags as minify_html
class MinifyHTMLMiddleware(object):
def process_response(self, request, response):
if 'text/html' in response['Content-Type']:
try:
response.content = minify_html(response.content.strip())
except DjangoUnicodeDecodeError:
pass
return response
...@@ -53,3 +53,17 @@ with the name “scripts”, you would use the following code to output them all ...@@ -53,3 +53,17 @@ with the name “scripts”, you would use the following code to output them all
{% compressed_css 'screen' %} {% compressed_css 'screen' %}
{% compressed_css 'print' %} {% compressed_css 'print' %}
{% compressed_js 'scripts' %} {% compressed_js 'scripts' %}
Middleware
==========
To enable HTML compression add ``compress.middleware.MinifyHTMLMiddleware``,
to your ``MIDDLEWARE_CLASSES`` settings.
Ensure that it comes after any middleware which modify your HTML, like ``GZipMiddleware`` ::
MIDDLEWARE_CLASSES = (
'django.middleware.gzip.GZipMiddleware',
'compress.middleware.MinifyHTMLMiddleware',
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment