Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

reactjs - react app showing empty page when redirecting on production build

I have a reactJs app which is running on a server that has nginx on it. The problem is that when I try to redirect from HomePage("/") to /UserPanel or /AdminPanel the url changes but it shows an empty page, but then when i reload the page, it works just fine. This problem doesn't occur in my localhost, but it happens to the production build.

this is my nginx config:

        listen 80 default_server;
        listen [::]:80 default_server;

       
        root /var/app/html;

        index index.html index.htm;

        location / {
                try_files $uri $uri/ /index.html?$args;
        }

And this is my main app class:

import { Route, Router, Switch } from "react-router-dom";

class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <Snackbar />
        <Router history={hist}>
          <MuiThemeProvider theme={theme}>
            <CssBaseline />
            <GlobalStyles />
            <Pace color={theme.palette.secondary.main} />
            <Suspense fallback={<Fragment />}>
              <Switch>
                <PrivateRoute
                  path="/AdminPanel"
                  component={AdminPanel}
                />
                <PrivateRoute
                  path="/UserPanel"
                  component={UserPanel}
                />
                <Route exact path="/" component={HomePage} />
              </Switch>
            </Suspense>
          </MuiThemeProvider>
        </Router>
      </Provider>
    );
  }
}

And I am redirecting from HomePage like this:

const redirect = () => {
    let url = isAdmin
      ? URLConstant.ADMIN_PANEL
      : isUser
      ? URLConstant.USER_PANEL
      : null;
    if (url) {
      return (
        <Redirect
          push
          to={{
            pathname: url,
            state: {}
          }}
        />
      );
    }
  };

And I'm using "react-router": "^5.2.0", "react-router-dom": "^5.2.0", "history": "latest".

What am I doing wrong? Should I add anything other than urls in .env? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Downgrade your "history" version or simply remove it because the correct version will be automatically added by "react-router-dom".

There is already an open issue about the same: https://github.com/ReactTraining/history/issues/804

Which says React Router (mainly history.push and Redirect) is not working properly with latest (v5) version of history. But it is working perfectly with v4 of history.

For example, this should work:

    "history": "^4.10.1",
    "react-router-dom": "^5.2.0",

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...