support non-ASCII filesystem bundle paths with metro server#1538
Closed
bradleyayers wants to merge 1 commit intofacebook:mainfrom
Closed
support non-ASCII filesystem bundle paths with metro server#1538bradleyayers wants to merge 1 commit intofacebook:mainfrom
bradleyayers wants to merge 1 commit intofacebook:mainfrom
Conversation
In a codebase with non-ascii characters in file paths (e.g. Japanese or Chinese characters), metro has an error when serving bundles: ``` TypeError: Invalid character in header content ``` This was reported in the Expo project expo/expo#27397 but it's not Expo code and was later closed. The problem is fairly simple—HTTP headers need to use ASCII characters only, but filesystems are not limited to ASCII so trying to put a filesystem path directly into a HTTP header value is going to cause a problem. The solution also seems relatively simple—URL encode the path so that it uses percent encoding of non-ASCII characters. I did three tests of this code, looking at the response headers for the chunk served by metro to look for backwards compatibility of using `new URL(…)` on paths that don't use non-ASCII characters. ``` with new URL(…) patch and /你/ path segment Content-Location: http://localhost:8081/projects/app/src/client/wiki/%E4%BD%A0/hello.bundle//&platform=web&dev=true&hot=false&lazy=true&transform.routerRoot=src%2Fapp&transform.reactCompiler=true&modulesOnly=true&runModule=false --- with new URL(…) and /ni/ path segment Content-Location: http://localhost:8081/projects/app/src/client/wiki/ni/hello.bundle//&platform=web&dev=true&hot=false&lazy=true&transform.routerRoot=src%2Fapp&transform.reactCompiler=true&modulesOnly=true&runModule=false --- original code with /ni/ path segment Content-Location: http://localhost:8081/projects/app/src/client/wiki/ni/hello.bundle//&platform=web&dev=true&hot=false&lazy=true&transform.routerRoot=src%2Fapp&transform.reactCompiler=true&modulesOnly=true&runModule=false ``` This demonstrates that it's backwards compatible, while properly encoding non-ASCII characters.
Contributor
vzaidman
pushed a commit
to vzaidman/metro
that referenced
this pull request
Aug 7, 2025
…#1538) Summary: In a codebase with non-ascii characters in file paths (e.g. Japanese or Chinese characters), metro has an error when serving bundles: ``` TypeError: Invalid character in header content ``` This was reported in the Expo project expo/expo#27397 but it's not Expo code and was later closed. The problem is fairly simple—HTTP headers need to use ASCII characters only, but filesystems are not limited to ASCII so trying to put a filesystem path directly into a HTTP header value is going to cause a problem. The solution also seems relatively simple—URL encode the path so that it uses percent encoding of non-ASCII characters. As suggested in https://velog.io/demian/RN-%EC%98%A4%EB%A5%98-%ED%95%B4%EA%B2%B0-TypeError-Invalid-character-in-header-content-X-React-Native-Project-Root, some people were adding the following middleware to fix that issue: ``` function createMetroStatusMiddleware(metroConfig) { return (_req, res)=>{ res.setHeader("X-React-Native-Project-Root", new URL(metroConfig.projectRoot)); res.end("packager-status:running"); }; } ``` Instead, I went to the root cause, and improved the parsing of the URL for both `sourceUrl` and `sourceMapUrl` fixing both the issue with serving bundles and the issue with serving source maps. > **_NOTE:_** this problem stems from using the [deprecated `url.parse` instead of the newer URL](https://nodejs.org/api/url.html#urlparseurlstring-parsequerystring-slashesdenotehost). Changelog: [General][Fixed] Metro supporting non-ASCII bundle/sourcemaps paths Pull Request resolved: facebook#1538 Test Plan: Using a test diff D78806803 I managed to reproduce Metro not launching RNTester before this diff. 1. `buck2 install -r rntester-android` (with the modified bundle path) 2. `DEV=1 js1 run` 3. launch `RNTester` Before- app crashes: https://pxl.cl/7LFkb After- app launches, debugging works: {F1980565661} can even add the bundle to ignore-list and then it gets ignored: {F1980565660} Differential Revision: D78799358
Contributor
Contributor
|
Note This PR was modified by @vzaidman and merged in 92ac74c with a more upstream decoding of URLs to prevent non-ASCII from crashing Metro. However, while Metro would now indeed not crash, Metro doesn't fully support non-ASCII files as of this PR. Instead, the later merged commit cb02cdb adds a comprehensive support to non-ASCII in Metro with tests. The split into two PRs is intended to credit the author of this PR and to allow us to revert the latter more complex code without removing this fix. |
52 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR was modified by @vzaidman and merged in 92ac74c with a more upstream decoding of URLs to prevent non-ASCII from crashing Metro.
However, while Metro would now indeed not crash, Metro doesn't fully support non-ASCII files as of this PR.
Instead, the later merged commit cb02cdb adds a comprehensive support to non-ASCII in Metro with tests.
The split into two PRs is intended to credit the author of this PR and to allow us to revert the latter more complex code without removing this fix.
Also see subsequent commits: 71e158a and 98badca
In a codebase with non-ascii characters in file paths (e.g. Japanese or Chinese characters), metro has an error when serving bundles:
This was reported in the Expo project expo/expo#27397 but it's not Expo code and was later closed.
The problem is fairly simple—HTTP headers need to use ASCII characters only, but filesystems are not limited to ASCII so trying to put a filesystem path directly into a HTTP header value is going to cause a problem.
The solution also seems relatively simple—URL encode the path so that it uses percent encoding of non-ASCII characters.
I did three tests of this code, looking at the response headers for the chunk served by metro to look for backwards compatibility of using
new URL(…)on paths that don't use non-ASCII characters.This demonstrates that it's backwards compatible, while properly encoding non-ASCII characters.
Summary
Changelog: [Fix] non-ascii files processed by Metro no longer crash the server