简体   繁体   English

托管在 GitHub 上的 React 路由器页面在刷新时中断

[英]React Router Page hosted on GitHub Breaks On Refresh

I have deployed a react app on GitHub the issue is when refresh is done on any of its routed pages I get 404 not found error我在 GitHub 上部署了一个反应应用程序,问题是在其任何路由页面上完成刷新时,我得到 404 not found 错误

在此处输入图像描述

though when the app is in development mode the pages refreshes properly虽然当应用程序处于开发模式时,页面会正确刷新

Link to the Published App链接到已发布的应用程序

i am using switch for routing我正在使用交换机进行路由

 < Switch > < Route exact path = "/" component = { Home } /> < Route exact path = "/todo" component = { Issuetracker } /> < Route exact path = "/weather" component = { Weather } /> < /Switch>

Kindly suggest way to fix it请提出解决方法

Usually it happens due to incorrect configurations in the nodejs server.通常它是由于 nodejs 服务器中的配置不正确而发生的。 You supposed to serve your react index.html file on every route (otherwise - you will get a 404 error).您应该在每条路线上提供您的反应index.html文件(否则 - 您将收到 404 错误)。 Like this:像这样:

app.get('/*', (req, res) => {
  const indexHtmlPath = path.join(__dirname, '..', 'build', 'index.html');
  res.sendFile(indexHtmlPath);
});

I was able to fix it by replacing browse route with hash route我能够通过用 hash 路由替换浏览路由来修复它

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

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