Skip to content

Commit cd162d2

Browse files
authored
Switch back to basic proxy only (#5072)
1 parent b8da584 commit cd162d2

File tree

1 file changed

+43
-84
lines changed

1 file changed

+43
-84
lines changed

packages/react-dev-utils/WebpackDevServerUtils.js

+43-84
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,15 @@ function prepareProxy(proxy, appPublicFolder) {
274274
if (!proxy) {
275275
return undefined;
276276
}
277-
if (typeof proxy !== 'object' && typeof proxy !== 'string') {
277+
if (typeof proxy !== 'string') {
278278
console.log(
279-
chalk.red(
280-
'When specified, "proxy" in package.json must be a string or an object.'
281-
)
279+
chalk.red('When specified, "proxy" in package.json must be a string.')
282280
);
283281
console.log(
284282
chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')
285283
);
286284
console.log(
287-
chalk.red(
288-
'Either remove "proxy" from package.json, or make it an object.'
289-
)
285+
chalk.red('Either remove "proxy" from package.json, or make it a string.')
290286
);
291287
process.exit(1);
292288
}
@@ -297,82 +293,42 @@ function prepareProxy(proxy, appPublicFolder) {
297293
return !fs.existsSync(maybePublicPath);
298294
}
299295

300-
// Support proxy as a string for those who are using the simple proxy option
301-
if (typeof proxy === 'string') {
302-
if (!/^http(s)?:\/\//.test(proxy)) {
303-
console.log(
304-
chalk.red(
305-
'When "proxy" is specified in package.json it must start with either http:// or https://'
306-
)
307-
);
308-
process.exit(1);
309-
}
310-
311-
let target;
312-
if (process.platform === 'win32') {
313-
target = resolveLoopback(proxy);
314-
} else {
315-
target = proxy;
316-
}
317-
return [
318-
{
319-
target,
320-
logLevel: 'silent',
321-
// For single page apps, we generally want to fallback to /index.html.
322-
// However we also want to respect `proxy` for API calls.
323-
// So if `proxy` is specified as a string, we need to decide which fallback to use.
324-
// We use a heuristic: We want to proxy all the requests that are not meant
325-
// for static assets and as all the requests for static assets will be using
326-
// `GET` method, we can proxy all non-`GET` requests.
327-
// For `GET` requests, if request `accept`s text/html, we pick /index.html.
328-
// Modern browsers include text/html into `accept` header when navigating.
329-
// However API calls like `fetch()` won’t generally accept text/html.
330-
// If this heuristic doesn’t work well for you, use a custom `proxy` object.
331-
context: function(pathname, req) {
332-
return (
333-
req.method !== 'GET' ||
334-
(mayProxy(pathname) &&
335-
req.headers.accept &&
336-
req.headers.accept.indexOf('text/html') === -1)
337-
);
338-
},
339-
onProxyReq: proxyReq => {
340-
// Browers may send Origin headers even with same-origin
341-
// requests. To prevent CORS issues, we have to change
342-
// the Origin to match the target URL.
343-
if (proxyReq.getHeader('origin')) {
344-
proxyReq.setHeader('origin', target);
345-
}
346-
},
347-
onError: onProxyError(target),
348-
secure: false,
349-
changeOrigin: true,
350-
ws: true,
351-
xfwd: true,
352-
},
353-
];
296+
if (!/^http(s)?:\/\//.test(proxy)) {
297+
console.log(
298+
chalk.red(
299+
'When "proxy" is specified in package.json it must start with either http:// or https://'
300+
)
301+
);
302+
process.exit(1);
354303
}
355304

356-
// Otherwise, proxy is an object so create an array of proxies to pass to webpackDevServer
357-
return Object.keys(proxy).map(function(context) {
358-
if (!proxy[context].hasOwnProperty('target')) {
359-
console.log(
360-
chalk.red(
361-
'When `proxy` in package.json is as an object, each `context` object must have a ' +
362-
'`target` property specified as a url string'
363-
)
364-
);
365-
process.exit(1);
366-
}
367-
let target;
368-
if (process.platform === 'win32') {
369-
target = resolveLoopback(proxy[context].target);
370-
} else {
371-
target = proxy[context].target;
372-
}
373-
return Object.assign({}, proxy[context], {
374-
context: function(pathname) {
375-
return mayProxy(pathname) && pathname.match(context);
305+
let target;
306+
if (process.platform === 'win32') {
307+
target = resolveLoopback(proxy);
308+
} else {
309+
target = proxy;
310+
}
311+
return [
312+
{
313+
target,
314+
logLevel: 'silent',
315+
// For single page apps, we generally want to fallback to /index.html.
316+
// However we also want to respect `proxy` for API calls.
317+
// So if `proxy` is specified as a string, we need to decide which fallback to use.
318+
// We use a heuristic: We want to proxy all the requests that are not meant
319+
// for static assets and as all the requests for static assets will be using
320+
// `GET` method, we can proxy all non-`GET` requests.
321+
// For `GET` requests, if request `accept`s text/html, we pick /index.html.
322+
// Modern browsers include text/html into `accept` header when navigating.
323+
// However API calls like `fetch()` won’t generally accept text/html.
324+
// If this heuristic doesn’t work well for you, use a custom `proxy` object.
325+
context: function(pathname, req) {
326+
return (
327+
req.method !== 'GET' ||
328+
(mayProxy(pathname) &&
329+
req.headers.accept &&
330+
req.headers.accept.indexOf('text/html') === -1)
331+
);
376332
},
377333
onProxyReq: proxyReq => {
378334
// Browers may send Origin headers even with same-origin
@@ -382,10 +338,13 @@ function prepareProxy(proxy, appPublicFolder) {
382338
proxyReq.setHeader('origin', target);
383339
}
384340
},
385-
target,
386341
onError: onProxyError(target),
387-
});
388-
});
342+
secure: false,
343+
changeOrigin: true,
344+
ws: true,
345+
xfwd: true,
346+
},
347+
];
389348
}
390349

391350
function choosePort(host, defaultPort) {

0 commit comments

Comments
 (0)