简体   繁体   中英

ReactJS production build with react-router not working

I have created simple app with react-router which part looks like:

                    <BrowserRouter>
                      <Switch>
                        <Route
                            exact
                            path="/"
                            render={() => <Redirect to="/home" />}
                        />
                        {layoutRoutes.map((prop, key) => {
                          return (
                              <Route
                                  path={prop.path}
                                  component={prop.component}
                                  key={key}
                              />
                          );
                        })}
                      </Switch>
                    </BrowserRouter>

but when the app is build and deployed to the web server is rendered as a white page and not even redirecting to the /home component.

The app itself was created via create-react-app and package.json looks like:

{
  "name": "front-end",
  "version": "0.1.0",
  "private": true,
  "homepage": ".",
  "dependencies": {
    "bootstrap": "^4.3.1",
    "formik": "^1.5.8",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-redux": "^7.1.1",
    "react-router-dom": "^5.0.1",
    "react-router-redux": "^4.0.8",
    "react-scripts": "3.1.1",
    "redux": "^4.0.4",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0",
    "yup": "^0.27.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "json-server": "^0.15.1"
  }
}

Any clue why is working on a development environment as npm start but not when build and deployed?

I have found that helpful:

If you're using Apache HTTP Server, you need to create a .htaccess file in the public folder that looks like this:

 Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME}.-f RewriteRule ^ index,html [QSA,L]

It will get copied to the build folder when you run npm run build .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM