ring-clojure / ring-defaults

A library to provide sensible Ring middleware defaults
MIT License
343 stars 32 forks source link

Add middleware that adds default charset utf-8 to text-based conten-types? #13

Closed Confusion closed 9 years ago

Confusion commented 9 years ago

When using ring and ring-defaults to serve static html, Firefox complains in its console that

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

w3 suggests that utf-8 is a reasonable default. I also interpret their comments there, and on linked pages, as saying that specifying utf-8 is better than defaulting to iso-8859-1 (which the absence of a charset implies), as utf-8 is more likely to be correct in more cases.

Currently everyone using ring, even with defaults, has to set the charset, even though it will be utf-8 in the majority of cases. Would it be an improvement for ring users if ring-defaults included a middleware that appends ;charset=utf-8 to all text-based content-types (text/*, application/json, ...), when no charset has been added to the response yet?

weavejester commented 9 years ago

A wrap-default-charset middleware may be useful, one that adds a charset if none is specified. The Ring-Headers library is probably the most suitable place for this. Ring-Defaults leverages existing libraries; by design it doesn't introduce any of its own middleware.