meeb/django-distill

Does minification work with django-distill?

Closed this issue ยท 9 comments

I have essentially copied your example site and started tweaking it (demo | repo),

I have django-htmlmin installed and it works just fine until I try to distill the site, then it is just ignored, and nothing is minified. I've tried different ideas from their docs to make it work for the distilled site as well, but I just cannot seem to figure anything out.

I'm just wondering if it is incompatible with django-distill, or if not, how would I make it work with the generated static site?

After I got this working, I was going to try using django-compressor for CSS, SASS and JS as well, but maybe you already know if this is is or is not compatible with django-distill too?

Thanks!

meeb commented

Internally django-distill uses parts of the Django testing framework to render HTML, so in theory it'll support anything that the Django renderer natively supports. I know tag-based in-template filters work (e.g. {% sass_src 'styles/distill.scss' %} for SASS) and middleware based filters should work.

I can only assume your minification process isn't triggered with Django's testing framework either for some reason. Can you check it with this:

$ python manage.py shell

and then in the python shell:

>>> from django.conf import settings
>>> settings.ALLOWED_HOSTS.append('testserver')
>>> from django.test import Client
>>> c = Client()
>>> response = c.get('/')
>>> print(resposen.content)

This should render the HTML generated by your / index page using the Django testing framework into the terminal. Check if that is minified. If it is minified, then the issue is probably somewhere in django-distill and how it calls the test framework, if it's not minified then it's probably something to do with django-htmlmin and not being triggered when running through the test framework.

Thank you for your response. I followed your instructions, and it is minified in the shell:

Screenshot from 2022-07-10 00-34-50

meeb commented

OK, can you try this as well? In the Django shell for your project:

>>> from django.conf import settings
>>> settings.ALLOWED_HOSTS.append('testserver')
>>> from django.test import RequestFactory
>>> from apps.blog.views import IndexView
>>> request_factory = RequestFactory()
>>> request = request_factory.get('/')
>>> response = IndexView.as_view()(request)
>>> response.render()
>>> print(response.content)
meeb commented

After looking into it this was caused by some middleware not being loaded in some situations. I've pushed a fix that will be in the next release. Thanks for the issue.

meeb commented

The middleware fix is now in the 2.10.0 release pushed to PyPI, please update with pip or whatever package manager you are using.

Thank you again for your help. I bumped the version and pushed the changes to my GitLab repo, but it still does not seem to minify anywhere but with runserver (and shell). I even tried using a production settings file with debug turned off in my Makefile, but it makes no difference. I also tried your second set of shell instructions, and it does still minify in the shell as well:

distill-minify-shell-test

distill-minify-shell-test-prod

meeb commented

Neither of your last screenshots have minification enabled, so that's showing the middleware isn't loaded properly. I've just pushed a minor tweak that might fix it. Please update to 2.10.1 from PyPi and try again.

Oh wow, that is perfect, it works now! Thank you so much!

meeb commented

No problem, glad you got it working.