Skip to content

Commit 62057e8

Browse files
gaearonromaindso
authored andcommitted
Files in public/ folder should not be requested through proxy (facebook#2326)
1 parent 2746db1 commit 62057e8

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

packages/react-dev-utils/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Returns a Promise resolving to either `defaultPort` or next available port if th
283283

284284
Creates a Webpack compiler instance for WebpackDevServer with built-in helpful messages. Takes the `require('webpack')` entry point as the first argument. To provide the `urls` argument, use `prepareUrls()` described below.
285285

286-
##### `prepareProxy(proxySetting: string): Object`
286+
##### `prepareProxy(proxySetting: string, appPublicFolder: string): Object`
287287

288288
Creates a WebpackDevServer `proxy` configuration object from the `proxy` setting in `package.json`.
289289

packages/react-dev-utils/WebpackDevServerUtils.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
'use strict';
1010

1111
const address = require('address');
12+
const fs = require('fs');
13+
const path = require('path');
1214
const url = require('url');
1315
const chalk = require('chalk');
1416
const detect = require('@timer/detect-port');
@@ -240,7 +242,7 @@ function onProxyError(proxy) {
240242
};
241243
}
242244

243-
function prepareProxy(proxy) {
245+
function prepareProxy(proxy, appPublicFolder) {
244246
// `proxy` lets you specify alternate servers for specific requests.
245247
// It can either be a string or an object conforming to the Webpack dev server proxy configuration
246248
// https://webpack.github.io/docs/webpack-dev-server.html
@@ -264,13 +266,11 @@ function prepareProxy(proxy) {
264266
process.exit(1);
265267
}
266268

267-
// Otherwise, if proxy is specified, we will let it handle any request.
268-
// There are a few exceptions which we won't send to the proxy:
269-
// - /index.html (served as HTML5 history API fallback)
270-
// - /*.hot-update.json (WebpackDevServer uses this too for hot reloading)
271-
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading)
272-
// Tip: use https://jex.im/regulex/ to visualize the regex
273-
const mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
269+
// Otherwise, if proxy is specified, we will let it handle any request except for files in the public folder.
270+
function mayProxy(pathname) {
271+
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
272+
return !fs.existsSync(maybePublicPath);
273+
}
274274

275275
// Support proxy as a string for those who are using the simple proxy option
276276
if (typeof proxy === 'string') {
@@ -301,7 +301,7 @@ function prepareProxy(proxy) {
301301
// However API calls like `fetch()` won’t generally accept text/html.
302302
// If this heuristic doesn’t work well for you, use a custom `proxy` object.
303303
context: function(pathname, req) {
304-
return mayProxy.test(pathname) &&
304+
return mayProxy(pathname) &&
305305
req.headers.accept &&
306306
req.headers.accept.indexOf('text/html') === -1;
307307
},
@@ -341,7 +341,7 @@ function prepareProxy(proxy) {
341341
}
342342
return Object.assign({}, proxy[context], {
343343
context: function(pathname) {
344-
return mayProxy.test(pathname) && pathname.match(context);
344+
return mayProxy(pathname) && pathname.match(context);
345345
},
346346
onProxyReq: proxyReq => {
347347
// Browers may send Origin headers even with same-origin

packages/react-scripts/scripts/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ choosePort(HOST, DEFAULT_PORT)
6666
const compiler = createCompiler(webpack, config, appName, urls, useYarn);
6767
// Load proxy config
6868
const proxySetting = require(paths.appPackageJson).proxy;
69-
const proxyConfig = prepareProxy(proxySetting);
69+
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
7070
// Serve webpack assets generated by the compiler over a web sever.
7171
const serverConfig = createDevServerConfig(
7272
proxyConfig,

0 commit comments

Comments
 (0)