|Reported by:||tonnzor||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
We need a simple middleware that would compactify all the pages before sending to the client. It should be basically the same as spaceless templatetag, but for whole response.
It will reduce page size significantly -- that is important for any production site. Middleware makes sense because you don't have to care about manually using this tag in templates. It makes sense even when you use Gzip as after decompression it needs to be parsed by browser -- so size still matters.
Here is the code. Nothing special, we just use Django internals.
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
The idea was taken from django-pipeline. But since it makes sense and it only uses Django internals -- it would be great to have it in Django itself.