Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router does not work in production and surge deployments

Tags:

My react application is working fine on localhost but when after i deployed it in gh-pages or surge it does not give me to move among pages using URL.

  • This is the project repo link
  • Demo url here

users can go to signup page by clicking menuItem of top right corner. but If user use http://chat.susi.ai/signup/ URL, it gives 404 page

I tried several solutions from internet but didn't work.

Next question is : I created a 404 page to show up when user tries to move to broken links.It works fine in localhost. but not in production. I tried this solution but nothing changed.

this is part of my index.js file

const App = () => (     <Router history={hashHistory}>         <MuiThemeProvider>             <Switch>                 <Route exact path="/" component={ChatApp} />                 <Route exact path="/signup" component={SignUp} />                 <Route exact path="/logout" component={Logout} />                 <Route exact path="/forgotpwd" component={ForgotPassword} />                 <Route exact path="*" component={NotFound} />              </Switch>         </MuiThemeProvider>     </Router> );  ReactDOM.render(     <App />,     document.getElementById('root') ); 

If someone can suggest good solution with sample code for my problems that would be really helpful for me.

like image 745
isuruAb Avatar asked Jun 12 '17 03:06

isuruAb


People also ask

Is React Router deprecated?

This feature has been deprecated because the new structure of Routes is that they should act like components, so you should take advantage of component lifecycle methods instead.

Why use React router dom instead of React router?

They are technically three different packages: React Router, React Router DOM, and React Router Native. The primary difference between them lies in their usage. React Router DOM is for web applications and React Router Native is for mobile applications made with React Native.

Does React router work with functional components?

Since the introduction of functional components, React has advanced a lot, for example, through the introduction of Hooks. Instead of normal class-based methods, Hooks allow the package to work well with functional components, and many packages are migrating in this direction. As a result, we see React Router Hooks.

How do I deploy the React app on surge?

First, we create a build of our project and then we write surge command on the terminal inside the build folder. We can even choose a custom name for our project. Your development environment is ready. Let us now install surge in our Application.


1 Answers

This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.

For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.


Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.

The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).

like image 146
Jonny Buchanan Avatar answered Nov 06 '22 06:11

Jonny Buchanan