简体   繁体   English

如何避免 react-router 出现 404 页面?

[英]How to avoid 404 page in react-router?

I'm working on a simple example using React Router.我正在研究一个使用 React Router 的简单示例。

I created it using create-react-app我使用 create-react-app 创建了它

On a local server, each link goes well ex) localhost:3000/login, localhost:3000/ product, etc.在本地服务器上,每个链接都运行良好,例如)localhost:3000/login、localhost:3000/product 等。

After deploying to my domain, I get a 404 error when I enter my link.部署到我的域后,输入链接时出现 404 错误。 ex) myDomain.com/login, myDomain.com/product例如)myDomain.com/login、myDomain.com/product

It looks good on the local server, so I think there's no problem with the source code.在本地服务器上看起来不错,所以我认为源代码没有问题。

Then, I received an answer to redirect to the index.html page when the 404, 403... etc page appears然后,当出现 404、403... 等页面时,我收到了重定向到 index.html 页面的答案

Are localhost:3000 and localhost:3000/index.html the same? localhost:3000 和 localhost:3000/index.html 是否相同?

On the main page (localhost: 3000 or myDomain.com), is well rendered.在主页(本地主机:3000 或 myDomain.com)上,呈现良好。

In index.html (localhost: 3000 / index.html or myDomain.com/index.html) it only renders up to h1 tag above {Home}.在 index.html (localhost: 3000 / index.html 或 myDomain.com/index.html) 中,它仅在 {Home} 上方呈现最多 h1 标记。 Is something wrong from here?从这里有什么问题吗?

Please help me TT请帮帮我TT

App.js应用程序.js


class App extends Component{
  render(){        
    return (
      <div className="App">
        <Header/>
        <h1>asd</h1>
        <h1>asd</h1>
        <Route exact path = "/" component = {Home}/>
          <Route path = "/about" component = {About}/>
          <Route path = "/event" component = {Event}/>
          <Route path = "/qna" component = {QnA}/>
          <Route path = "/login" component = {Login}/>
          <Route path = "/join" component = {Join}/>
          <Route path = "/product" component = {Product}/>
      </div>
    );
  }
}

export default App;

Root.js根.js


const Root = () => {
    return (
        <BrowserRouter>
            <App/>
        </BrowserRouter>

    );
};

export default Root;

You can wrap your route components in a switch component and render a default component if none match like below.您可以将路由组件包装在 switch 组件中,如果没有匹配项,则呈现默认组件,如下所示。

<Switch>
    <Route exact path = "/" component = {Home}/>
    <Route path = "/about" component = {About}/>
    <Route path = "/event" component = {Event}/>
    <Route path = "/qna" component = {QnA}/>
    <Route path = "/login" component = {Login}/>
    <Route path = "/join" component = {Join}/>
    <Route path = "/product" component = {Product}/>
    <Route component={NoMatch} />
</Switch>

Obviously you'd change the NoMatch component with whatever you'd want your default component to be.显然,您可以使用您想要的默认组件来更改NoMatch组件。

You need to enable Server side rendering for your app, when you reload the /mydomain.com/login page, server is looking for that particular route which doesn't exist on server side, the only route the server knows is the / .您需要为您的应用程序启用服务器端渲染,当您重新加载/mydomain.com/login页面时,服务器正在寻找服务器端不存在的特定路由,服务器知道的唯一路由是/

In your case routing is handled by browser unless Server side rendering is enable.在您的情况下,除非启用服务器端渲染,否则路由由浏览器处理。

and No, localhost:3000 and localhost:3000/index are not the same route in react app.不, localhost:3000localhost:3000/index在反应应用程序中不是同一条路线。

With react-router v4, you can handle 404's in two ways:使用 react-router v4,您可以通过两种方式处理 404:

  1. Keep the path and show 404 page:保留路径并显示 404 页面:
<Switch>
    <Route exact path="/" component={MyComponent} />
    <Route component={GenericNotFound} />
</Switch>
  1. Change the path (redirect) and show a 404 page:更改路径(重定向)并显示 404 页面:
<Switch>
    <Route path="/users" component={MyComponent} />
    <Redirect to="/404" />
</Switch>

You should add the basename to the BrowserRouter component in the root您应该将基本名称添加到根中的 BrowserRouter 组件

ReactDOM.render(
  <BrowserRouter basename='/My-App/'>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </BrowserRouter>
  ,
  document.getElementById('root')
);

Also, you should make sure in 404 cases you are redirected to index.html because it's a SPA and everything is happening there, you do this by simply replacing this line with "build" script in package.json此外,您应该确保在 404 种情况下您被重定向到 index.html 因为它是一个 SPA 并且一切都在那里发生,您只需将此行替换为 package.Z466DEEC76ECDF24D154F3 中的“构建”脚本即可

 "build": "react-scripts build && cp build/index.html build/404.html",

I don't think this is your react app issue, rather it's how your app is deployed.我不认为这是您的反应应用程序问题,而是您的应用程序的部署方式。

I hope you have deployed your app using production build (using npm run build ).我希望您已经使用生产构建部署了您的应用程序(使用npm run build )。 If so, this is most likely that your web server is not redirecting to the right entry.如果是这样,这很可能是您的 web 服务器没有重定向到正确的条目。

You need to tell your web server(nginx or caddy whatever you are using) to point to /index.html when hitting 404. Here is a simple config for nginx.您需要告诉您的 web 服务器(无论您使用的是 nginx 还是 caddy)指向/index.html时达到 404。这是 ZEE434023CF89D7DFB21F63D64F0FD9 的简单配置。

https://www.barrydobson.com/post/react-router-nginx/ https://www.barrydobson.com/post/react-router-nginx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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