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

fix(react-scripts): do not redirect served path if request may proxy #8442

Merged
merged 1 commit into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-dev-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ The `args` object accepts a number of properties:
- **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.
- **webpack** `function`: A reference to the webpack constructor.

##### `prepareProxy(proxySetting: string, appPublicFolder: string): Object`
##### `prepareProxy(proxySetting: string, appPublicFolder: string, servedPathname: string): Object`

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

Expand Down
7 changes: 5 additions & 2 deletions packages/react-dev-utils/WebpackDevServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function onProxyError(proxy) {
};
}

function prepareProxy(proxy, appPublicFolder) {
function prepareProxy(proxy, appPublicFolder, servedPathname) {
// `proxy` lets you specify alternate servers for specific requests.
if (!proxy) {
return undefined;
Expand All @@ -380,7 +380,10 @@ function prepareProxy(proxy, appPublicFolder) {
const sockPath = process.env.WDS_SOCKET_PATH || '/sockjs-node';
const isDefaultSockHost = !process.env.WDS_SOCKET_HOST;
function mayProxy(pathname) {
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
const maybePublicPath = path.resolve(
appPublicFolder,
pathname.replace(new RegExp('^' + servedPathname), '')
);
const isPublicFileRequest = fs.existsSync(maybePublicPath);
// used by webpackHotDevClient
const isWdsEndpointRequest =
Expand Down
8 changes: 5 additions & 3 deletions packages/react-scripts/config/webpackDevServer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ module.exports = function(proxy, allowedHost) {
index: paths.publicUrlOrPath,
},
public: allowedHost,
// `proxy` is run between `before` and `after` `webpack-dev-server` hooks
proxy,
before(app, server) {
// Keep `evalSourceMapMiddleware` and `errorOverlayMiddleware`
Expand All @@ -117,13 +118,14 @@ module.exports = function(proxy, allowedHost) {
// This lets us open files from the runtime error overlay.
app.use(errorOverlayMiddleware());

// Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
app.use(redirectServedPath(paths.publicUrlOrPath));

if (fs.existsSync(paths.proxySetup)) {
// This registers user provided middleware for proxy reasons
require(paths.proxySetup)(app);
}
},
after(app) {
// Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
app.use(redirectServedPath(paths.publicUrlOrPath));

// This service worker file is effectively a 'no-op' that will reset any
// previous service worker registered for the same host:port combination.
Expand Down
6 changes: 5 additions & 1 deletion packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ checkBrowsers(paths.appPath, isInteractive)
});
// Load proxy config
const proxySetting = require(paths.appPackageJson).proxy;
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
const proxyConfig = prepareProxy(
proxySetting,
paths.appPublic,
paths.publicUrlOrPath
);
// Serve webpack assets generated by the compiler over a web server.
const serverConfig = createDevServerConfig(
proxyConfig,
Expand Down