Add charset=UTF-8 to Content-Type headers for text-based MIME types - #347
Conversation
…ntent-Type headers Co-authored-by: brianhuster <111893501+brianhuster@users.noreply.github.com>
Co-authored-by: brianhuster <111893501+brianhuster@users.noreply.github.com>
Co-authored-by: brianhuster <111893501+brianhuster@users.noreply.github.com>
Co-authored-by: brianhuster <111893501+brianhuster@users.noreply.github.com>
|
@copilot Remove changes in the tests folder. The Kanji could just be added to existing HTML file |
Co-authored-by: brianhuster <111893501+brianhuster@users.noreply.github.com>
| ["mj2"] = "video/mj2", | ||
| ["mjp2"] = "video/mj2", | ||
| ["mjs"] = "text/javascript", | ||
| ["mjs"] = "text/javascript; charset=UTF-8", |
There was a problem hiding this comment.
@copilot Is it necessary to have "charset=UTF-8" here? (This is a question, not a request)
There was a problem hiding this comment.
Yes, it's necessary. Without charset=UTF-8 in the Content-Type header, browsers may default to ISO-8859-1 or other legacy encodings when interpreting text-based files. This causes Unicode characters (Chinese, Japanese, Korean, emoji, etc.) to render as garbled text like Ã,Æ—‡Å instead of 你好世界.
For JavaScript files specifically, this ensures that string literals and comments containing Unicode characters are properly interpreted. While modern browsers often auto-detect UTF-8, the explicit charset declaration guarantees correct rendering across all browsers and prevents encoding issues.
There was a problem hiding this comment.
@copilot Should the encoding be got from the buffer-local option 'encoding'?
There was a problem hiding this comment.
No, that wouldn't work in this context. The HTTP server serves files directly from disk using async_read_file, not from Neovim buffers. The content_type.lua module runs in the server's context where buffer-local options like encoding or fileencoding aren't available.
Additionally, UTF-8 is the standard encoding for web content. Files on disk are typically UTF-8 encoded (especially for web development), and hardcoding charset=UTF-8 in the HTTP response ensures consistent browser interpretation regardless of the file's actual encoding on disk.
HTML files containing Chinese, Japanese, Korean, or other Unicode characters render as garbled text because the HTTP server omits charset from Content-Type headers, causing browsers to default to ISO-8859-1.
Changes
Modified
lua/livepreview/server/utils/content_type.lua:; charset=UTF-8to 11 text-based MIME types:text/html(html, htm)application/xhtml+xml(xhtml, xht)text/css(css)text/javascript(js, mjs)application/xml(xml)image/svg+xml(svg, svgz)text/plain(txt)Example
Before:
HTTP response causes browser to misinterpret UTF-8:
Result:
你好世界→Ã,Æ—‡ÅAfter:
HTTP response explicitly declares UTF-8:
Result:
你好世界→你好世界Testing
Added
tests/index.htmlwith Kanji and Japanese characters to test Unicode rendering.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.