Skip to content

Server: Add a charset to Content-Type for text assets and sources - #1888

Open
robhogan wants to merge 1 commit into
mainfrom
pr1888
Open

Server: Add a charset to Content-Type for text assets and sources#1888
robhogan wants to merge 1 commit into
mainfrom
pr1888

Conversation

@robhogan

Copy link
Copy Markdown
Collaborator

Summary

Metro's dev server sets Content-Type with mime.lookup(), both for /assets/ and for source files under /[metro-project]/. That returns a bare type like text/html with no charset, so clients fall back to their own default - #859 is a WebView rendering a UTF-8 .html asset as mojibake.

mime-types has contentType() for this. It appends ; charset=utf-8 where a charset applies and is otherwise identical, including returning false for an unrecognised extension, so binary assets are unaffected.

Fixes: #859

Changelog:

 - **[Fix]**: Include `charset=utf-8` in the `Content-Type` of text assets and source files served by the dev server

Test plan

yarn jest packages/metro/src/Server
yarn flow check

New cases for a text asset, a source file and a binary file. The charset assertions fail on main.

## Summary

Metro's dev server sets `Content-Type` with `mime.lookup()`, both for `/assets/` and for source files under `/[metro-project]/`. That returns a bare type like `text/html` with no charset, so clients fall back to their own default - #859 is a `WebView` rendering a UTF-8 `.html` asset as mojibake.

`mime-types` has `contentType()` for this. It appends `; charset=utf-8` where a charset applies and is otherwise identical, including returning `false` for an unrecognised extension, so binary assets are unaffected.

Fixes: #859

Changelog:
```
 - **[Fix]**: Include `charset=utf-8` in the `Content-Type` of text assets and source files served by the dev server
```

## Test plan

```
yarn jest packages/metro/src/Server
yarn flow check
```

New cases for a text asset, a source file and a binary file. The charset assertions fail on `main`.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 30, 2026
@meta-codesync

meta-codesync Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D118437775.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-ascii binary data as assets may not properly serialize

1 participant