Skip to content

Commit 8d5b816

Browse files
committed
Re-enable hosting inside a back-end application
We often host React apps in a back-end application. In order for that to work, we need to customize the configuration in several places. In 1.0, we turned the webpack dev configuration into a function that took `publicPath` as an argument. In this version of CRA, the dev config is imported in more than one place, and not all places have the necessary information. So, instead, we now duplicate the code for computing the public path. This is unfortunate, but does work. This version also makes it so that we can open the browser to the correct location (if the host application sets the `APP_PORT` env var appropriately), so we re-enable that code.
1 parent 44e7ab9 commit 8d5b816

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,26 @@ const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1919
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
2020
const eslintFormatter = require('react-dev-utils/eslintFormatter');
2121
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
22+
const { prepareUrls } = require('react-dev-utils/WebpackDevServerUtils');
2223
const getClientEnvironment = require('./env');
2324
const paths = require('./paths');
2425

26+
// ZEAL: We often host our React apps within a back-end application. Thus,
27+
// we need to provide the correct location of the webpack dev-server to its
28+
// client. If we don't, it will use `window.location` instead, which will be
29+
// pointing at the back-end application.
30+
// NOTE: It is important to keep this code in sync with `scripts/start.js`.
31+
// The duplication is unfortunate, but unavoidable with the current structure
32+
// of the upstream code.
33+
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
34+
const host = process.env.HOST || '0.0.0.0';
35+
const port = parseInt(process.env.PORT, 10) || 3000;
36+
const urls = prepareUrls(protocol, host, port);
37+
2538
// Webpack uses `publicPath` to determine where the app is being served from.
2639
// In development, we always serve from the root. This makes config easier.
27-
const publicPath = '/';
40+
const publicPath = urls.localUrlForBrowser;
41+
2842
// `publicUrl` is just like `publicPath`, but we will provide it to our app
2943
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3044
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ module.exports = function(proxy, allowedHost) {
8686
// See https://github.com/facebookincubator/create-react-app/issues/387.
8787
disableDotRule: true,
8888
},
89+
headers: {
90+
// ZEAL: Allow cross-origin requests so that, when hosted in a back-end
91+
// application, the client can fetch assets and hot module reloading
92+
// updates.
93+
'Access-Control-Allow-Origin': '*',
94+
},
8995
public: allowedHost,
9096
proxy,
9197
setup(app) {

packages/react-scripts/scripts/start.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ choosePort(HOST, DEFAULT_PORT)
6464
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
6565
const appName = require(paths.appPackageJson).name;
6666
const urls = prepareUrls(protocol, HOST, port);
67+
// ZEAL: We often host our React apps within a back-end application. Thus,
68+
// we need to open the browser on that application and not the webpack
69+
// dev server. If the client application specifies `APP_PORT`, we'll open
70+
// the browser on that port instead of the dev server's port.
71+
const appPort = parseInt(process.env.APP_PORT, 10) || port;
72+
const appUrls = prepareUrls(protocol, HOST, appPort);
6773
// Create a webpack compiler that is configured with custom messages.
68-
const compiler = createCompiler(webpack, config, appName, urls, useYarn);
74+
// ZEAL: Pass `appUrls` instead of `urls` to see proper instructions.
75+
const compiler = createCompiler(webpack, config, appName, appUrls, useYarn);
6976
// Load proxy config
7077
const proxySetting = require(paths.appPackageJson).proxy;
7178
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
@@ -84,7 +91,9 @@ choosePort(HOST, DEFAULT_PORT)
8491
clearConsole();
8592
}
8693
console.log(chalk.cyan('Starting the development server...\n'));
87-
openBrowser(urls.localUrlForBrowser);
94+
// ZEAL: Use appUrls instead of urls to allow for back-end host
95+
// application.
96+
openBrowser(appUrls.localUrlForBrowser);
8897
});
8998

9099
['SIGINT', 'SIGTERM'].forEach(function(sig) {

0 commit comments

Comments
 (0)