Skip to content

Add charset=utf-8 to Content-Type for CSS and HTML assets#264

Open
flavorjones wants to merge 3 commits intorails:mainfrom
flavorjones:fix-css-content-type
Open

Add charset=utf-8 to Content-Type for CSS and HTML assets#264
flavorjones wants to merge 3 commits intorails:mainfrom
flavorjones:fix-css-content-type

Conversation

@flavorjones
Copy link
Copy Markdown
Member

@flavorjones flavorjones commented Apr 14, 2026

Note: this branch was based on #265 to get CI green.

Motivation / Background

CSS files served by Propshaft::Server have Content-Type: text/css with no charset=utf-8. When Chrome discovers a stylesheet via <link> tag during HTML parsing (rather than via Link: rel=preload header), it may inherit the document's encoding or fall back to Windows-1252 instead of defaulting to UTF-8. This causes non-ASCII characters in CSS (e.g., en-dash U+2013 in content properties) to render as mojibake.

The same issue affects HTML assets.

The CSS spec defaults to UTF-8, but Chrome has a longstanding bug where it doesn't reliably follow this default. Adding charset=utf-8 to the Content-Type header is the highest-priority encoding signal in the spec's detection chain.

Companion Rails PR: rails/rails#57188

Detail

Propshaft::Server#call now appends ; charset=utf-8 to the Content-Type header for text/css and text/html assets. Only these two MIME types are affected — text/javascript is excluded per RFC 9239, text/xml could break encoding declarations, and text/plain has no demonstrated browser bug.

Additional information

ActionDispatch::Response already adds charset=utf-8 unconditionally to all controller responses, but Propshaft::Server builds its own Rack response and bypasses that path. This fix closes that gap for CSS and HTML assets in development.

Minitest 6 split minitest/mock into a separate gem, which breaks
tests that require "minitest/mock". Pin to < 6 in the Gemfiles
where this causes failures.

Rails 7.2 is unaffected (it constrains minitest itself).
@flavorjones

This comment was marked as outdated.

CSS and HTML assets served by Propshaft::Server now include
"; charset=utf-8" in their Content-Type response header. This fixes
browser encoding issues where non-ASCII characters in CSS files (e.g.,
en-dash in content properties) render as mojibake when Chrome falls back
to Windows-1252 encoding.
@flavorjones flavorjones force-pushed the fix-css-content-type branch from c5cbc65 to d28d07f Compare April 14, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant