diff --git a/packages/react-scripts/config/webpackDevServer.config.js b/packages/react-scripts/config/webpackDevServer.config.js index 2202da20346..c9e72ac4428 100644 --- a/packages/react-scripts/config/webpackDevServer.config.js +++ b/packages/react-scripts/config/webpackDevServer.config.js @@ -90,8 +90,10 @@ module.exports = function (proxy, allowedHost) { // remove last slash so user can land on `/test` instead of `/test/` publicPath: paths.publicUrlOrPath.slice(0, -1), }, - - https: getHttpsConfig(), + server: !getHttpsConfig() ? 'http' : { + type: 'https', + options: getHttpsConfig(), + }, host, historyApiFallback: { // Paths with dots should still use the history fallback. @@ -101,27 +103,37 @@ module.exports = function (proxy, allowedHost) { }, // `proxy` is run between `before` and `after` `webpack-dev-server` hooks proxy, - onBeforeSetupMiddleware(app, server) { + setupMiddlewares(middlewares, devServer) { // Keep `evalSourceMapMiddleware` // middlewares before `redirectServedPath` otherwise will not have any effect // This lets us fetch source contents from webpack for the error overlay - app.use(evalSourceMapMiddleware(server)); + middlewares.unshift({ + name: 'evalSourceMapMiddleware', + middleware: evalSourceMapMiddleware(devServer), + }); if (fs.existsSync(paths.proxySetup)) { // This registers user provided middleware for proxy reasons require(paths.proxySetup)(app); } - }, - onAfterSetupMiddleware(app) { + // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match - app.use(redirectServedPath(paths.publicUrlOrPath)); + middlewares.push({ + name: 'redirectServedPath', + middleware: 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. // We do this in development to avoid hitting the production cache if // it used the same host and port. // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432 - app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath)); + middlewares.push({ + name: 'noopServiceWorkerMiddleware', + middleware: noopServiceWorkerMiddleware(paths.publicUrlOrPath), + }); + + return middlewares; }, }; };