I have a react app, created using create-react-app.
After I run npm run build
, and deploy the app as a static site, everything works except when I refresh the page on a route other than the index, it fails with a 500 internal server error
.
Below is the code for my router. Using react-router.
<Router history={browserHistory}>
<Route path="/">
<IndexRoute component={Login}/>
<Route path="/dashboard" component={Dashboard}></Route>
<Route path="/profile" component={Profile}/>
</Router>
Any help appreciated.
To solve 500 HTTP Internal Server Error, reload a web page. You can do that by clicking the refresh/reload button, pressing F5 or Ctrl + R, or trying the URL from the address bar again.
The 500 Internal Server error could be caused by an error during the execution of any policy within Edge or by an error on the target/backend server. The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request.
Solution 1 A 500 internal server error just means something went wrong with your code. You'll need to examine your server logs to find out what the problem is and fix it. But there are plenty of problems with the code you've shown. Your code is vulnerable to SQL Injection[^].
When you’re visiting a route of your app directly, e.g. via https://myapp.com/route/123 Apache tries to map that URL to a file in the public folder. In this case it looks for /route/123.html which obviously doesn’t exist. To avoid that, we have to tell Apache to redirect all requests to our index.html so our app can perform some routing magic for that URL. To tell Apache to redirect requests to index.html where our app lives, we have to modify the .htaccess file. If there is no such file in your app’s folder yet, just create it.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
for more information check this out.
Your server's route should match the routes defined in the react-router, If they don't match then you will receive a 500 error.
Can you share some error log screenshot or copy-paste ? 500 is a server-side error, obviously. Maybe your routes on server are not matching the url pattern. Or some express server route function threw an exception.
Please, provide some logs from client and\or server.
UPD: Just mentioned the "static site" thing. Didn't understand what exactly do you mean by that. For me it's no server at all.
Still i'm pretty sure that your server has no routes configured.
Server knows what is "/"("index.html"). But there are no routes configured for, say, "/potatoes".
In express server you would do something like:
app.get('*', function(req, res){
res.sendFile(__dirname + '/index.html');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With