Skip to content

Commit 1fa352d

Browse files
committed
Enable custom sockjs pathname for hot reloading server.
1 parent 94932be commit 1fa352d

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

docusaurus/docs/advanced-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You can adjust various development and production settings by setting environmen
1414
| HOST | ✅ Used | 🚫 Ignored | By default, the development web server binds to all hostnames on the device (`localhost`, LAN network address, etc.). You may use this variable to specify a different host. |
1515
| PORT | ✅ Used | 🚫 Ignored | By default, the development web server will attempt to listen on port 3000 or prompt you to attempt the next available port. You may use this variable to specify a different port. |
1616
| HTTPS | ✅ Used | 🚫 Ignored | When set to `true`, Create React App will run the development server in `https` mode. |
17+
| WDS_SOCKET_PATH | ✅ Used | 🚫 Ignored | When set to `/pathname`, Create React App will run the development server with a custom websocket path for hot module reloading. Normally, webpack-dev-server defaults to `/sockjs-node` for the sockjs pathname. You may use this variable to start local development on more than one create-react-app project at one time. |
1718
| PUBLIC_URL | 🚫 Ignored | ✅ Used | Create React App assumes your application is hosted at the serving web server's root or a subpath as specified in [`package.json` (`homepage`)](deployment#building-for-relative-paths). Normally, Create React App ignores the hostname. You may use this variable to force assets to be referenced verbatim to the url you provide (hostname included). This may be particularly useful when using a CDN to host your application. |
1819
| CI | ✅ Used | ✅ Used | When set to `true`, Create React App treats warnings as failures in the build. It also makes the test runner non-watching. Most CIs set this flag by default. |
1920
| REACT_EDITOR | ✅ Used | 🚫 Ignored | When an app crashes in development, you will see an error overlay with clickable stack trace. When you click on it, Create React App will try to determine the editor you are using based on currently running processes, and open the relevant source file. You can [send a pull request to detect your editor of choice](https://github.com/facebook/create-react-app/issues/2636). Setting this environment variable overrides the automatic detection. If you do it, make sure your systems [PATH](<https://en.wikipedia.org/wiki/PATH_(variable)>) environment variable points to your editor’s bin folder. You can also set it to `none` to disable it completely. |

packages/react-dev-utils/WebpackDevServerUtils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,11 @@ function prepareProxy(proxy, appPublicFolder) {
377377
// If proxy is specified, let it handle any request except for
378378
// files in the public folder and requests to the WebpackDevServer socket endpoint.
379379
// https://github.com/facebook/create-react-app/issues/6720
380+
const sockPath = process.env.WDS_SOCKET_PATH || '/sockjs-node';
380381
function mayProxy(pathname) {
381382
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
382383
const isPublicFileRequest = fs.existsSync(maybePublicPath);
383-
const isWdsEndpointRequest = pathname.startsWith('/sockjs-node'); // used by webpackHotDevClient
384+
const isWdsEndpointRequest = pathname.startsWith(sockPath); // used by webpackHotDevClient
384385
return !(isPublicFileRequest || isWdsEndpointRequest);
385386
}
386387

packages/react-dev-utils/webpackHotDevClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var connection = new WebSocket(
6363
hostname: window.location.hostname,
6464
port: window.location.port,
6565
// Hardcoded in WebpackDevServer
66-
pathname: '/sockjs-node',
66+
pathname: process.env.WDS_SOCKET_PATH || '/sockjs-node',
6767
slashes: true,
6868
})
6969
);

packages/react-scripts/config/env.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ function getClientEnvironment(publicUrl) {
8585
// This should only be used as an escape hatch. Normally you would put
8686
// images into the `src` and `import` them in code to get their paths.
8787
PUBLIC_URL: publicUrl,
88+
// We support configuring the sockjs pathname during development.
89+
// This lets a developer run multiple simultaneous projects.
90+
// It is used as the connection `pathname` in webpackHotDevClient.
91+
// It is used as the `sockPath` option in webpack dev server.
92+
WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH || '/sockjs-node',
8893
}
8994
);
9095
// Stringify all values so we can feed into Webpack DefinePlugin

packages/react-scripts/config/webpackDevServer.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const fs = require('fs');
1717

1818
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
1919
const host = process.env.HOST || '0.0.0.0';
20+
const sockPath = process.env.WDS_SOCKET_PATH || undefined; // default: '/sockjs-node'
2021

2122
module.exports = function(proxy, allowedHost) {
2223
return {
@@ -60,7 +61,7 @@ module.exports = function(proxy, allowedHost) {
6061
contentBase: paths.appPublic,
6162
// By default files from `contentBase` will not trigger a page reload.
6263
watchContentBase: true,
63-
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
64+
// Enable hot reloading server. It will provide WDS_SOCKET_PATH endpoint
6465
// for the WebpackDevServer client so it can learn when the files were
6566
// updated. The WebpackDevServer client is included as an entry point
6667
// in the Webpack development configuration. Note that only changes
@@ -72,6 +73,8 @@ module.exports = function(proxy, allowedHost) {
7273
// Prevent a WS client from getting injected as we're already including
7374
// `webpackHotDevClient`.
7475
injectClient: false,
76+
// Enable custom sockjs pathname for websocket connection to hot reloading server.
77+
sockPath,
7578
// It is important to tell WebpackDevServer to use the same "root" path
7679
// as we specified in the config. In development, we always serve from /.
7780
publicPath: '/',

0 commit comments

Comments
 (0)