I'm using React-router and it works fine while I'm clicking into link buttons, but when I refresh my webpage it does not load what I want.
For instance, I am into localhost/joblist and everything is fine because I arrived here pressing a link. But If I refresh the webpage I get: Cannot GET /joblist
By default It didn't work like this. Initially I had my URL: localhost/#/ and localhost/#/joblist and they worked perfectly fine. But I don't like this kind of url, so trying to erase that '#' I wrote:
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.body);
});
This problem does not happen with localhost/, this one always returns what I want.
EDIT: This app is single-page, so /joblist don't need to ask anything to any server.
EDIT2: My entire router.
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="joblist" path="/joblist" handler={JobList}/>
<DefaultRoute handler={Dashboard}/>
<NotFoundRoute handler={NotFound}/>
</Route>
);
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.body);
});
If you are using Express or some other framework in the backend , you can add the similar configuration as below and check out the Webpack public path in the configuration, it should work fine even on reload if you are using BrowserRouter
I had this same problem and this solution worked for us..
Background:
We are hosting multiple apps on the same server. When we would refresh the server would not understand where to look for our index in the dist folder for that particular app. The above link will take you to what worked for us... Hope this helps, as we have spent quite a hours on figuring out a solution for our needs.
We are using:
my webpack.config.js
my index.js
my app.js
Its pretty simple when you got cannot get 403 error after refresh dom component. just add this one line in your web pack config, 'historyApiFallback: true '. this savez my whole day.
This can solve your problem
I also faced the same problem in ReactJS application in Production mode. Here is the 2 solution to the problem.
1.Change the routing history to "hashHistory" instead of browserHistory in the place of
Now build the app using the command
Then place the build folder in your var/www/ folder, Now the application is working fine with addition of # tag in each and every url. like
localhost/#/home localhost/#/aboutus
Solution 2 : Without # tag using browserHistory,
Set your history = {browserHistory} in your Router,Now build it using sudo npm run build.
You need to create the "conf" file to solve the 404 not found page, the conf file should be like this.
open your terminal type the below commands
cd /etc/apache2/sites-available ls nano sample.conf Add the below content in it.
Now you need to enable the sample.conf file by using the following command
then it will ask you to reload the apache server,using sudo service apache2 reload or restart
then open your localhost/build folder and add the .htaccess file with content of below.
Now the app is working normally.
Note: change 0.0.0.0 ip to your local ip address.
If any doubts regarding this feel free to raise a comment.
I hope it is helpful to others.
The answers here are all extremely helpful, what worked for me was configuring my Webpack server to expect the routes.
The historyApiFallback is what fixed this issue for me. Now routing works correctly and I can refresh the page or type in the URL directly. No need to worry about work arounds on your node server. This answer obviously only works if you're using webpack.
EDIT: see my answer here for a more detailed reason why this is necessary: https://stackoverflow.com/a/37622953/5217568
I used create-react-app to make a website just now and had the same issue presented here. I use
BrowserRouting
from thereact-router-dom
package. I am running on a Nginx server and what solved it for me was adding the following to/etc/nginx/yourconfig.conf
Which corresponds to adding the following to the
.htaccess
in case you are running AppacheThis also seems to be the solution suggested by Facebook themselves and can be found here