Skip to content

Commit d45823c

Browse files
author
Andrew Luca
authoredFeb 8, 2020
fix(react-scripts): do not redirect served path if request may proxy (#8442)
Moved redirect middleware and noopSW middleware in WDS after hook So proxy, and before proxy will take precedence before redirect Closes #8417
1 parent eb8e7be commit d45823c

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed
 

‎packages/react-dev-utils/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ The `args` object accepts a number of properties:
345345
- **tscCompileOnError** `boolean`: If `true`, errors in TypeScript type checking will not prevent start script from running app, and will not cause build script to exit unsuccessfully. Also downgrades all TypeScript type checking error messages to warning messages.
346346
- **webpack** `function`: A reference to the webpack constructor.
347347

348-
##### `prepareProxy(proxySetting: string, appPublicFolder: string): Object`
348+
##### `prepareProxy(proxySetting: string, appPublicFolder: string, servedPathname: string): Object`
349349

350350
Creates a WebpackDevServer `proxy` configuration object from the `proxy` setting in `package.json`.
351351

‎packages/react-dev-utils/WebpackDevServerUtils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function onProxyError(proxy) {
356356
};
357357
}
358358

359-
function prepareProxy(proxy, appPublicFolder) {
359+
function prepareProxy(proxy, appPublicFolder, servedPathname) {
360360
// `proxy` lets you specify alternate servers for specific requests.
361361
if (!proxy) {
362362
return undefined;
@@ -380,7 +380,10 @@ function prepareProxy(proxy, appPublicFolder) {
380380
const sockPath = process.env.WDS_SOCKET_PATH || '/sockjs-node';
381381
const isDefaultSockHost = !process.env.WDS_SOCKET_HOST;
382382
function mayProxy(pathname) {
383-
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
383+
const maybePublicPath = path.resolve(
384+
appPublicFolder,
385+
pathname.replace(new RegExp('^' + servedPathname), '')
386+
);
384387
const isPublicFileRequest = fs.existsSync(maybePublicPath);
385388
// used by webpackHotDevClient
386389
const isWdsEndpointRequest =

‎packages/react-scripts/config/webpackDevServer.config.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ module.exports = function(proxy, allowedHost) {
108108
index: paths.publicUrlOrPath,
109109
},
110110
public: allowedHost,
111+
// `proxy` is run between `before` and `after` `webpack-dev-server` hooks
111112
proxy,
112113
before(app, server) {
113114
// Keep `evalSourceMapMiddleware` and `errorOverlayMiddleware`
@@ -117,13 +118,14 @@ module.exports = function(proxy, allowedHost) {
117118
// This lets us open files from the runtime error overlay.
118119
app.use(errorOverlayMiddleware());
119120

120-
// Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
121-
app.use(redirectServedPath(paths.publicUrlOrPath));
122-
123121
if (fs.existsSync(paths.proxySetup)) {
124122
// This registers user provided middleware for proxy reasons
125123
require(paths.proxySetup)(app);
126124
}
125+
},
126+
after(app) {
127+
// Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
128+
app.use(redirectServedPath(paths.publicUrlOrPath));
127129

128130
// This service worker file is effectively a 'no-op' that will reset any
129131
// previous service worker registered for the same host:port combination.

‎packages/react-scripts/scripts/start.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ checkBrowsers(paths.appPath, isInteractive)
122122
});
123123
// Load proxy config
124124
const proxySetting = require(paths.appPackageJson).proxy;
125-
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
125+
const proxyConfig = prepareProxy(
126+
proxySetting,
127+
paths.appPublic,
128+
paths.publicUrlOrPath
129+
);
126130
// Serve webpack assets generated by the compiler over a web server.
127131
const serverConfig = createDevServerConfig(
128132
proxyConfig,

0 commit comments

Comments
 (0)
Please sign in to comment.