I have...
Back-End
Node.js/express server which serves files when requests are made to certain routes.
Front-End
React pages that makes calls to the back-end requesting data to populate the pages.
I'm using react-router v4 on the front end. Whenever I navigate to a route that is NOT at the exact path AND reload the page, I get a 404 error. I understand why this isn't working; the browser makes a request to a path handled by react-router, and since it doesn't find that route on the server, I get 404.
I'm seeking for a solution to this problem.
// BrowserRouter imported as Router
<Router>
<Route exact path='/main' component={Main} />
<Route path='/sub1' component={SubOne} />
<Route path='/sub2' component={SubTwo} />
</Router>
When I go to /main
in the browser, <Main />
is rendered. Say that inside <Main />
, there are links to /sub1
and /sub2
respectively. I click on /sub2
. Component and page content renders without fail. Then I refresh page, either by accident or intentionally (say component Sub2 lifts state up to Main).
How do I avoid getting 404 after this refresh? How do I get the page/component where "I was" rendered after a refresh if I'm using React-Router?
create-react-app cli
it automatically does this for you. However, if you used your own boilerplate, you need to use webpack to handle 404 fallbacks. – Curious13publicPath: '/'
specified in your webpack output. – Dennis