简体   繁体   中英

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

在此处输入图像描述

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. You supposed to serve your react index.html file on every route (otherwise - you will get a 404 error). 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

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