Skip to content

Commit 8a81c82

Browse files
committed
Add proxy option to package.json
1 parent 31e6cf1 commit 8a81c82

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"babel-runtime": "6.11.6",
4343
"case-sensitive-paths-webpack-plugin": "1.1.2",
4444
"chalk": "1.1.3",
45+
"connect-history-api-fallback": "1.2.0",
4546
"cross-spawn": "4.0.0",
4647
"css-loader": "0.23.1",
4748
"detect-port": "0.1.4",
@@ -57,6 +58,7 @@
5758
"fs-extra": "0.30.0",
5859
"gzip-size": "3.0.0",
5960
"html-webpack-plugin": "2.22.0",
61+
"http-proxy-middleware": "0.17.0",
6062
"json-loader": "0.5.4",
6163
"opn": "4.0.2",
6264
"postcss-loader": "0.9.1",

scripts/start.js

+54-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ var path = require('path');
1313
var chalk = require('chalk');
1414
var webpack = require('webpack');
1515
var WebpackDevServer = require('webpack-dev-server');
16+
var historyApiFallback = require('connect-history-api-fallback');
17+
var httpProxyMiddleware = require('http-proxy-middleware');
1618
var execSync = require('child_process').execSync;
1719
var opn = require('opn');
1820
var detect = require('detect-port');
1921
var prompt = require('./utils/prompt');
2022
var config = require('../config/webpack.config.dev');
23+
var paths = require('../config/paths');
2124

2225
// Tools like Cloud9 rely on this
2326
var DEFAULT_PORT = process.env.PORT || 3000;
@@ -150,16 +153,64 @@ function openBrowser(port) {
150153
opn('http://localhost:' + port + '/');
151154
}
152155

156+
function addMiddleware(devServer) {
157+
// `proxy` lets you to specify a fallback server during development.
158+
// Every unrecognized request will be forwarded to it.
159+
var proxy = require(paths.appPackageJson).proxy;
160+
devServer.use(historyApiFallback({
161+
// For single page apps, we generally want to fallback to /index.html.
162+
// However we also want to respect `proxy` for API calls.
163+
// So if `proxy` is specified, we need to decide which fallback to use.
164+
// We use a heuristic: if request `accept`s text/html, we pick /index.html.
165+
// Modern browsers include text/html into `accept` header when navigating.
166+
// However API calls like `fetch()` won’t generally won’t accept text/html.
167+
// If this heuristic doesn’t work well for you, don’t use `proxy`.
168+
htmlAcceptHeaders: proxy ?
169+
['text/html'] :
170+
['text/html', '*/*']
171+
}));
172+
if (proxy) {
173+
if (typeof proxy !== 'string') {
174+
console.log(chalk.red('When specified, "proxy" in package.json must be a string.'));
175+
console.log(chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".'));
176+
console.log(chalk.red('Either remove "proxy" from package.json, or make it a string.'));
177+
process.exit(1);
178+
}
179+
180+
// Otherwise, if proxy is specified, we will let it handle any request.
181+
// There are a few exceptions which we won't send to the proxy:
182+
// - /index.html (served as HTML5 history API fallback)
183+
// - /*.hot-update.json (WebpackDevServer uses this too for hot reloading)
184+
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading)
185+
var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
186+
devServer.use(mayProxy,
187+
// Pass the scope regex both to Express and to the middleware for proxying
188+
// of both HTTP and WebSockets to work without false positives.
189+
httpProxyMiddleware(pathname => mayProxy.test(pathname), {
190+
target: proxy,
191+
logLevel: 'silent',
192+
secure: false,
193+
changeOrigin: true,
194+
ws: true
195+
})
196+
);
197+
}
198+
// Finally, by now we have certainly resolved the URL.
199+
// It may be /index.html, so let the dev server try serving it again.
200+
devServer.use(devServer.middleware);
201+
}
202+
153203
function runDevServer(port) {
154-
new WebpackDevServer(compiler, {
155-
historyApiFallback: true,
204+
var devServer = new WebpackDevServer(compiler, {
156205
hot: true, // Note: only CSS is currently hot reloaded
157206
publicPath: config.output.publicPath,
158207
quiet: true,
159208
watchOptions: {
160209
ignored: /node_modules/
161210
}
162-
}).listen(port, (err, result) => {
211+
});
212+
addMiddleware(devServer);
213+
devServer.listen(port, (err, result) => {
163214
if (err) {
164215
return console.log(err);
165216
}

0 commit comments

Comments
 (0)