Unverified Commit cd162d20 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub
Browse files

Switch back to basic proxy only (#5072)

parent b8da5849
Showing with 43 additions and 84 deletions
+43 -84
...@@ -274,19 +274,15 @@ function prepareProxy(proxy, appPublicFolder) { ...@@ -274,19 +274,15 @@ function prepareProxy(proxy, appPublicFolder) {
if (!proxy) { if (!proxy) {
return undefined; return undefined;
} }
if (typeof proxy !== 'object' && typeof proxy !== 'string') { if (typeof proxy !== 'string') {
console.log( console.log(
chalk.red( chalk.red('When specified, "proxy" in package.json must be a string.')
'When specified, "proxy" in package.json must be a string or an object.'
)
); );
console.log( console.log(
chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".') chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')
); );
console.log( console.log(
chalk.red( chalk.red('Either remove "proxy" from package.json, or make it a string.')
'Either remove "proxy" from package.json, or make it an object.'
)
); );
process.exit(1); process.exit(1);
} }
...@@ -297,82 +293,42 @@ function prepareProxy(proxy, appPublicFolder) { ...@@ -297,82 +293,42 @@ function prepareProxy(proxy, appPublicFolder) {
return !fs.existsSync(maybePublicPath); return !fs.existsSync(maybePublicPath);
} }
// Support proxy as a string for those who are using the simple proxy option if (!/^http(s)?:\/\//.test(proxy)) {
if (typeof proxy === 'string') { console.log(
if (!/^http(s)?:\/\//.test(proxy)) { chalk.red(
console.log( 'When "proxy" is specified in package.json it must start with either http:// or https://'
chalk.red( )
'When "proxy" is specified in package.json it must start with either http:// or https://' );
) process.exit(1);
);
process.exit(1);
}
let target;
if (process.platform === 'win32') {
target = resolveLoopback(proxy);
} else {
target = proxy;
}
return [
{
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.
// We use a heuristic: We want to proxy all the requests that are not meant
// for static assets and as all the requests for static assets will be using
// `GET` method, we can proxy all non-`GET` requests.
// For `GET` requests, if request `accept`s text/html, we pick /index.html.
// Modern browsers include text/html into `accept` header when navigating.
// However API calls like `fetch()` won’t generally accept text/html.
// If this heuristic doesn’t work well for you, use a custom `proxy` object.
context: function(pathname, req) {
return (
req.method !== 'GET' ||
(mayProxy(pathname) &&
req.headers.accept &&
req.headers.accept.indexOf('text/html') === -1)
);
},
onProxyReq: proxyReq => {
// Browers may send Origin headers even with same-origin
// requests. To prevent CORS issues, we have to change
// the Origin to match the target URL.
if (proxyReq.getHeader('origin')) {
proxyReq.setHeader('origin', target);
}
},
onError: onProxyError(target),
secure: false,
changeOrigin: true,
ws: true,
xfwd: true,
},
];
} }
// Otherwise, proxy is an object so create an array of proxies to pass to webpackDevServer let target;
return Object.keys(proxy).map(function(context) { if (process.platform === 'win32') {
if (!proxy[context].hasOwnProperty('target')) { target = resolveLoopback(proxy);
console.log( } else {
chalk.red( target = proxy;
'When `proxy` in package.json is as an object, each `context` object must have a ' + }
'`target` property specified as a url string' return [
) {
); target,
process.exit(1); logLevel: 'silent',
} // For single page apps, we generally want to fallback to /index.html.
let target; // However we also want to respect `proxy` for API calls.
if (process.platform === 'win32') { // So if `proxy` is specified as a string, we need to decide which fallback to use.
target = resolveLoopback(proxy[context].target); // We use a heuristic: We want to proxy all the requests that are not meant
} else { // for static assets and as all the requests for static assets will be using
target = proxy[context].target; // `GET` method, we can proxy all non-`GET` requests.
} // For `GET` requests, if request `accept`s text/html, we pick /index.html.
return Object.assign({}, proxy[context], { // Modern browsers include text/html into `accept` header when navigating.
context: function(pathname) { // However API calls like `fetch()` won’t generally accept text/html.
return mayProxy(pathname) && pathname.match(context); // If this heuristic doesn’t work well for you, use a custom `proxy` object.
context: function(pathname, req) {
return (
req.method !== 'GET' ||
(mayProxy(pathname) &&
req.headers.accept &&
req.headers.accept.indexOf('text/html') === -1)
);
}, },
onProxyReq: proxyReq => { onProxyReq: proxyReq => {
// Browers may send Origin headers even with same-origin // Browers may send Origin headers even with same-origin
...@@ -382,10 +338,13 @@ function prepareProxy(proxy, appPublicFolder) { ...@@ -382,10 +338,13 @@ function prepareProxy(proxy, appPublicFolder) {
proxyReq.setHeader('origin', target); proxyReq.setHeader('origin', target);
} }
}, },
target,
onError: onProxyError(target), onError: onProxyError(target),
}); secure: false,
}); changeOrigin: true,
ws: true,
xfwd: true,
},
];
} }
function choosePort(host, defaultPort) { function choosePort(host, defaultPort) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment