django-critical
django-critical copied to clipboard
UnicodeDecodeError
Hi, great tool. Thanks! :)
On my page, i get an unicode decode error, even though my page is utf-8 too.
'ascii' codec can't decode byte 0xc3 in position 754: ordinal not in range(128)
Environment:
Request Method: GET Request URL: http://127.0.0.1:8000/
Django Version: 1.6.5 Python Version: 2.7.6 Installed Applications: ['south', 'djcelery', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', 'django.contrib.flatpages', 'django.contrib.admin', 'static_sitemaps', 'critical', 'jato', 'config', 'profiles', 'dealers', 'pricing', 'crm', 'utils', 'landing_pages', 'seo', 'wordpress'] Installed Middleware: ('django.middleware.gzip.GZipMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'djangoflash.middleware.FlashMiddleware', 'config.middleware.AcceptMiddleware', 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', 'htmlmin.middleware.HtmlMinifyMiddleware', 'htmlmin.middleware.MarkRequestMiddleware', 'critical.middleware.CriticalCssMiddleware')
Traceback: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
-
File "/Library/Python/2.7/site-packages/critical/middleware.py" in process_responseresponse = middleware_method(request, response)
-
File "/Library/Python/2.7/site-packages/critical/core.py" in get_critical_csscritical_css = get_critical_css(response.content, css)
-
File "/Library/Python/2.7/site-packages/critical/core.py" in runreturn PenthouseCommand().run(html, css)
-
htmlfile.write(html.encode(self.encoding))
Exception Type: UnicodeDecodeError at / Exception Value: 'ascii' codec can't decode byte 0xc3 in position 754: ordinal not in range(128)
Thank you for reporting this. I will have to write a few test cases and review the way CriticalCssMiddleware handles Unicode, it's a bit messy.
Hi Martin, can't wait. ;-)
Not sure why, but I can't post an issue anymore. In general, I think it great be to have a management command that one can run during deployment. It should e.g. determine the critical CSS once for each url in your urls.py and create a rewritten version of the actual django template (e.g., template.critical.html). Like this, you can avoid the additional harddrive reads that the CSS snippets do require and the additional rendering that is currently needed. Especially for pages, where performance is key, this would be of great advantage. Alternatively, you could of course rewrite (and rename) the the django templates at first request.