Skip to content

Commit 6727d4a

Browse files
lexunbmatto
authored andcommitted
Fix reloading when served from a custom backend
Before these changes, when the bundle was served from a custom web server, requests for updates were being sent to the port seen on window rather than what the webpack dev server was running on.
1 parent 1c2ab8c commit 6727d4a

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ var paths = require('./paths');
2222

2323
// Webpack uses `publicPath` to determine where the app is being served from.
2424
// In development, we always serve from the root. This makes config easier.
25-
var publicPath = '/';
25+
// ZEAL: Setting publicPath in the start script and passing it in. Since we are
26+
// mounting this app on various backends, the dev server port will be different
27+
// from the port on window location. Because of this, we need the full public
28+
// path, not just the relative path. Elements of the full path can be dynamic,
29+
// but are all known in the start script, making it the best place to define the
30+
// public path.
31+
// var publicPath = '/';
32+
2633
// `publicUrl` is just like `publicPath`, but we will provide it to our app
2734
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
2835
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
@@ -33,7 +40,9 @@ var env = getClientEnvironment(publicUrl);
3340
// This is the development configuration.
3441
// It is focused on developer experience and fast rebuilds.
3542
// The production configuration is different and lives in a separate file.
36-
module.exports = {
43+
// ZEAL: Converted to a function to allow injecting the publicPath.
44+
module.exports = function(publicPath) {
45+
return {
3746
// This makes the bundle appear split into separate modules in the devtools.
3847
// We don't use source maps here because they can be confusing:
3948
// https://github.com/facebookincubator/create-react-app/issues/343#issuecomment-237241875
@@ -51,9 +60,12 @@ module.exports = {
5160
// Note: instead of the default WebpackDevServer client, we use a custom one
5261
// to bring better experience for Create React App users. You can replace
5362
// the line below with these two lines if you prefer the stock client:
54-
// require.resolve('webpack-dev-server/client') + '?/',
55-
// require.resolve('webpack/hot/dev-server'),
56-
require.resolve('react-dev-utils/webpackHotDevClient'),
63+
// ZEAL: Opted to use the default client because the custom one gets the
64+
// port off window location, which will be different from the dev server
65+
// when the app is served from a different back end.
66+
require.resolve('webpack-dev-server/client') + '?' + publicPath,
67+
require.resolve('webpack/hot/dev-server'),
68+
// require.resolve('react-dev-utils/webpackHotDevClient'),
5769
// We ship a few polyfills by default:
5870
require.resolve('./polyfills'),
5971
// Finally, this is your app's code:
@@ -221,4 +233,4 @@ module.exports = {
221233
net: 'empty',
222234
tls: 'empty'
223235
}
224-
};
236+
}};

packages/react-scripts/scripts/start.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ if (isSmokeTest) {
5757
function setupCompiler(host, port, protocol) {
5858
// "Compiler" is a low-level interface to Webpack.
5959
// It lets us listen to some events and provide our own custom messages.
60-
compiler = webpack(config, handleCompile);
60+
// ZEAL: Injecting the publicPath into the config since it needs to be fully
61+
// qualified now. More notes in the config regarding publicPath.
62+
compiler = webpack(config(publicPath(host, port, protocol)), handleCompile);
6163

6264
// "invalid" event fires when you have changed a file, and Webpack is
6365
// recompiling a bundle. WebpackDevServer takes care to pause serving the
@@ -221,7 +223,9 @@ function runDevServer(host, port, protocol) {
221223
hot: true,
222224
// It is important to tell WebpackDevServer to use the same "root" path
223225
// as we specified in the config. In development, we always serve from /.
224-
publicPath: config.output.publicPath,
226+
// ZEAL: The public path is now being injected into the config with this
227+
// function, so no need to reach into the config to get it anymore.
228+
publicPath: publicPath(host, port, protocol),
225229
// WebpackDevServer is noisy by default so we emit custom message instead
226230
// by listening to the compiler events with `compiler.plugin` calls above.
227231
quiet: true,
@@ -251,6 +255,10 @@ function runDevServer(host, port, protocol) {
251255
});
252256
}
253257

258+
function publicPath(host, port, protocol) {
259+
return protocol + '://' + host + ':' + port + '/'
260+
}
261+
254262
function run(port) {
255263
var protocol = process.env.HTTPS === 'true' ? "https" : "http";
256264
var host = process.env.HOST || 'localhost';

0 commit comments

Comments
 (0)