Skip to content

Commit 3d7e138

Browse files
Kudofacebook-github-bot
authored andcommitted
Fix port as -1 if dev server without specifying port on Android (#34705)
Summary: when specifying dev server without port, e.g. http://www.example.com/, there are some issues. 1. redbox error <img src="https://user-images.githubusercontent.com/46429/190540390-8ee420f2-7642-427b-9f2e-e0c6d31015f8.png" width="30%"> 2. showing -1 in loading view <img src="https://user-images.githubusercontent.com/46429/190540727-158f35ad-359f-443a-a4b0-768dd2f7e400.png" width="50%"> the root cause is coming from [`java.net.URL.getPort()` will return -1 when the url doesn't have a port](https://developer.android.com/reference/java/net/URL#getPort()). this pr replaces the parser to [`okhttp3.HttpUrl`](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-http-url/#port) that it will have default port 80 for http or port 443 for https. the two call paths should only serve http/https address, not file:// address. it should be safe to change from java.net.URL to okhttp3.HttpUrl. not fully related, in the case above, android will connect to `ws://www.example.com/:8097` for react-devtools we should strip the trailing slash in *setUpReactDevTools.js* ## Changelog [Android] [Fixed] - Fix port as -1 if dev server without specifying port on Android Pull Request resolved: #34705 Test Plan: test on rn-tester with the following steps 1. `yarn start` 2. open another terminal and run `ngrok http 8081` and it will return a tunnel url, e.g. `71a1-114-36-194-97.jp.ngrok.io` 3. open dev setting in app and change the dev server to `71a1-114-36-194-97.jp.ngrok.io` 5. reload the app Reviewed By: cipolleschi Differential Revision: D39573988 Pulled By: cortinico fbshipit-source-id: 397df90ab30533207bd87a3f069132d97c22c7fd
1 parent c2b699a commit 3d7e138

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Libraries/Core/setUpReactDevTools.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ if (__DEV__) {
3939
// Get hostname from development server (packager)
4040
const devServer = getDevServer();
4141
const host = devServer.bundleLoadedFromServer
42-
? devServer.url.replace(/https?:\/\//, '').split(':')[0]
42+
? devServer.url
43+
.replace(/https?:\/\//, '')
44+
.replace(/\/$/, '')
45+
.split(':')[0]
4346
: 'localhost';
4447

4548
// Read the optional global variable for backward compatibility.

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public void showForUrl(String url) {
6969
return;
7070
}
7171

72+
int port = parsedURL.getPort() != -1 ? parsedURL.getPort() : parsedURL.getDefaultPort();
7273
showMessage(
73-
context.getString(
74-
R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + parsedURL.getPort()));
74+
context.getString(R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + port));
7575
}
7676

7777
public void showForRemoteJSEnabled() {

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ private void resetCurrentContext(@Nullable ReactContext reactContext) {
703703
URL sourceUrl = new URL(getSourceUrl());
704704
String path = sourceUrl.getPath().substring(1); // strip initial slash in path
705705
String host = sourceUrl.getHost();
706-
int port = sourceUrl.getPort();
706+
int port = sourceUrl.getPort() != -1 ? sourceUrl.getPort() : sourceUrl.getDefaultPort();
707707
mCurrentContext
708708
.getJSModule(HMRClient.class)
709709
.setup("android", path, host, port, mDevSettings.isHotModuleReplacementEnabled());

0 commit comments

Comments
 (0)