diff --git a/docusaurus/docs/advanced-configuration.md b/docusaurus/docs/advanced-configuration.md index 871edd2f49d..ab7f6ca8e2e 100644 --- a/docusaurus/docs/advanced-configuration.md +++ b/docusaurus/docs/advanced-configuration.md @@ -14,6 +14,9 @@ You can adjust various development and production settings by setting environmen | 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. | | 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. | | HTTPS | βœ… Used | 🚫 Ignored | When set to `true`, Create React App will run the development server in `https` mode. | +| WDS_SOCKET_HOST | βœ… Used | 🚫 Ignored | When set, Create React App will run the development server with a custom websocket hostname for hot module reloading. Normally, `webpack-dev-server` defaults to `window.location.hostname` for the SockJS hostname. You may use this variable to start local development on more than one Create React App project at a time. See [webpack-dev-server documentation](https://webpack.js.org/configuration/dev-server/#devserversockhost) for more details. | +| WDS_SOCKET_PATH | βœ… Used | 🚫 Ignored | When set, 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 a time. See [webpack-dev-server documentation](https://webpack.js.org/configuration/dev-server/#devserversockpath) for more details. | +| WDS_SOCKET_PORT | βœ… Used | 🚫 Ignored | When set, Create React App will run the development server with a custom websocket port for hot module reloading. Normally, `webpack-dev-server` defaults to `window.location.port` for the SockJS port. You may use this variable to start local development on more than one Create React App project at a time. See [webpack-dev-server documentation](https://webpack.js.org/configuration/dev-server/#devserversockport) for more details. | | 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. | | 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. | | 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]() environment variable points to your editor’s bin folder. You can also set it to `none` to disable it completely. | diff --git a/packages/react-dev-utils/WebpackDevServerUtils.js b/packages/react-dev-utils/WebpackDevServerUtils.js index d43f141c387..a4d45cc30d1 100644 --- a/packages/react-dev-utils/WebpackDevServerUtils.js +++ b/packages/react-dev-utils/WebpackDevServerUtils.js @@ -377,10 +377,14 @@ function prepareProxy(proxy, appPublicFolder) { // If proxy is specified, let it handle any request except for // files in the public folder and requests to the WebpackDevServer socket endpoint. // /~https://github.com/facebook/create-react-app/issues/6720 + const sockPath = process.env.WDS_SOCKET_PATH || '/sockjs-node'; + const isDefaultSockHost = !process.env.WDS_SOCKET_HOST; function mayProxy(pathname) { const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1)); const isPublicFileRequest = fs.existsSync(maybePublicPath); - const isWdsEndpointRequest = pathname.startsWith('/sockjs-node'); // used by webpackHotDevClient + // used by webpackHotDevClient + const isWdsEndpointRequest = + isDefaultSockHost && pathname.startsWith(sockPath); return !(isPublicFileRequest || isWdsEndpointRequest); } diff --git a/packages/react-dev-utils/webpackHotDevClient.js b/packages/react-dev-utils/webpackHotDevClient.js index b669fd9d001..8592df4497f 100644 --- a/packages/react-dev-utils/webpackHotDevClient.js +++ b/packages/react-dev-utils/webpackHotDevClient.js @@ -60,10 +60,10 @@ if (module.hot && typeof module.hot.dispose === 'function') { var connection = new WebSocket( url.format({ protocol: window.location.protocol === 'https:' ? 'wss' : 'ws', - hostname: window.location.hostname, - port: window.location.port, + hostname: process.env.WDS_SOCKET_HOST || window.location.hostname, + port: process.env.WDS_SOCKET_PORT || window.location.port, // Hardcoded in WebpackDevServer - pathname: '/sockjs-node', + pathname: process.env.WDS_SOCKET_PATH || '/sockjs-node', slashes: true, }) ); diff --git a/packages/react-scripts/config/env.js b/packages/react-scripts/config/env.js index 36c731a9d1b..2969143fa44 100644 --- a/packages/react-scripts/config/env.js +++ b/packages/react-scripts/config/env.js @@ -85,6 +85,14 @@ function getClientEnvironment(publicUrl) { // This should only be used as an escape hatch. Normally you would put // images into the `src` and `import` them in code to get their paths. PUBLIC_URL: publicUrl, + // We support configuring the sockjs pathname during development. + // These settings let a developer run multiple simultaneous projects. + // They are used as the connection `hostname`, `pathname` and `port` + // in webpackHotDevClient. They are used as the `sockHost`, `sockPath` + // and `sockPort` options in webpack-dev-server. + WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST, + WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH, + WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT, } ); // Stringify all values so we can feed into Webpack DefinePlugin diff --git a/packages/react-scripts/config/webpackDevServer.config.js b/packages/react-scripts/config/webpackDevServer.config.js index b1b14a5ffda..c8b0d8b186c 100644 --- a/packages/react-scripts/config/webpackDevServer.config.js +++ b/packages/react-scripts/config/webpackDevServer.config.js @@ -17,6 +17,9 @@ const fs = require('fs'); const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'; const host = process.env.HOST || '0.0.0.0'; +const sockHost = process.env.WDS_SOCKET_HOST; +const sockPath = process.env.WDS_SOCKET_PATH; // default: '/sockjs-node' +const sockPort = process.env.WDS_SOCKET_PORT; module.exports = function(proxy, allowedHost) { return { @@ -60,7 +63,7 @@ module.exports = function(proxy, allowedHost) { contentBase: paths.appPublic, // By default files from `contentBase` will not trigger a page reload. watchContentBase: true, - // Enable hot reloading server. It will provide /sockjs-node/ endpoint + // Enable hot reloading server. It will provide WDS_SOCKET_PATH endpoint // for the WebpackDevServer client so it can learn when the files were // updated. The WebpackDevServer client is included as an entry point // in the Webpack development configuration. Note that only changes @@ -72,6 +75,12 @@ module.exports = function(proxy, allowedHost) { // Prevent a WS client from getting injected as we're already including // `webpackHotDevClient`. injectClient: false, + // Enable custom sockjs pathname for websocket connection to hot reloading server. + // Enable custom sockjs hostname, pathname and port for websocket connection + // to hot reloading server. + sockHost, + sockPath, + sockPort, // It is important to tell WebpackDevServer to use the same "root" path // as we specified in the config. In development, we always serve from /. publicPath: '/',