django-critical icon indicating copy to clipboard operation
django-critical copied to clipboard

UnicodeDecodeError

Open programmdesign opened this issue 10 years ago • 3 comments

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

  1.             response = middleware_method(request, response)
    
    File "/Library/Python/2.7/site-packages/critical/middleware.py" in process_response
  2.         critical_css = get_critical_css(response.content, css)
    
    File "/Library/Python/2.7/site-packages/critical/core.py" in get_critical_css
  3. return PenthouseCommand().run(html, css)
    
    File "/Library/Python/2.7/site-packages/critical/core.py" in run
  4.         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)

programmdesign avatar Oct 29 '14 23:10 programmdesign

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.

martinblech avatar Oct 30 '14 13:10 martinblech

Hi Martin, can't wait. ;-)

programmdesign avatar Nov 01 '14 13:11 programmdesign

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.

programmdesign avatar Nov 01 '14 19:11 programmdesign