Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default behavior for both String and Object proxy option in package.json #3369

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1439,4 +1439,4 @@ Please [file an issue](https://github.com/facebookincubator/create-react-app/iss

## Releases Before 0.x

Please refer to [CHANGELOG-0.x.md](./CHANGELOG-0.x.md) for earlier versions.
Please refer to [CHANGELOG-0.x.md](./CHANGELOG-0.x.md) for earlier versions.
21 changes: 12 additions & 9 deletions packages/react-dev-utils/WebpackDevServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ function prepareProxy(proxy, appPublicFolder) {
process.exit(1);
}

const defaultProxyOption = {
logLevel: 'silent',
secure: false,
changeOrigin: true,
ws: true,
xfwd: true,
};

// Otherwise, if proxy is specified, we will let it handle any request except for files in the public folder.
function mayProxy(pathname) {
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
Expand All @@ -311,9 +319,8 @@ function prepareProxy(proxy, appPublicFolder) {
target = proxy;
}
return [
{
Object.assign({}, defaultProxyOption, {
target,
logLevel: 'silent',
// For single page apps, we generally want to fallback to /index.html.
// However we also want to respect `proxy` for API calls.
// So if `proxy` is specified as a string, we need to decide which fallback to use.
Expand All @@ -337,11 +344,7 @@ function prepareProxy(proxy, appPublicFolder) {
}
},
onError: onProxyError(target),
secure: false,
changeOrigin: true,
ws: true,
xfwd: true,
},
}),
];
}

Expand All @@ -362,7 +365,8 @@ function prepareProxy(proxy, appPublicFolder) {
} else {
target = proxy[context].target;
}
return Object.assign({}, proxy[context], {
return Object.assign({}, defaultProxyOption, proxy[context], {
target,
context: function(pathname) {
return mayProxy(pathname) && pathname.match(context);
},
Expand All @@ -374,7 +378,6 @@ function prepareProxy(proxy, appPublicFolder) {
proxyReq.setHeader('origin', target);
}
},
target,
onError: onProxyError(target),
});
});
Expand Down