How can I configure the encoding of static files in aiohttp?

How can I ensure that my static files served during development are using utf-8?

I have a static handler like so:

app.router.add_static('/static', MY_STATIC_DIR)

But, I've noticed that utf-8 characters show up garbled in the browser during development. The same files show up fine when served via nginx in production.


The content in question lives in a webpack .js file. I've tested serving this bundle with both nginx and node and the problem goes away. But, to keep development simple, I'd like to use just aiohttp rather than having to spin up a second process.

1 answer

  • answered 2018-01-12 13:46 Andrew Svetlov

    Content encoding is determined by Content-Type header which in turn uses mimetype.guess_type for finding a type based on file extension.

    It does nothing with text character encoding. Figuring out plain text encoding is quite expensive procedure (need processing reading file content for tool like chardet) and not 100% stable (a file can contain multiple encoding).


    Static file support in aiohttp is for handling resources like images, not human readable plain texts. HTML should be rendered correctly though.