Weird behavior when handle NotFound with server-side rendering
I'm doing server side rendering and I share the code between server and client sides. I simply handle NotFoundRoute by rendering "Not found" text
Here's the routes
<Route name="root" path="/" handler={require('./handlers/Root')}>
<DefaultRoute handler={require('./handlers/Home')} />
<Route name="charts" handler={require('./handlers/Charts')} />
<Redirect name="old-charts" from="old-charts" to="charts" />
<NotFoundRoute name="not-found" handler={require('./handlers/NotFound')}/>
</Route>
When I click on a wrong url (Last link in the list below) it redirects to /fu and it shows not found, instead of rendering it using javascript (without loading the whole /fu page).
<ul>
<li><Link to="root">Home</Link></li>
<li><Link to="charts">Charts</Link></li>
<li><Link to="old-charts" query={{foo: 'bar'}}>Old -> Charts</Link></li>
<li><a href="/fu">Errr URL -> NotFound</a></li>
</ul>
The application works as expected for the other parts like redirects.
Here's how I handle the routes/request in server
var router = Router.create({
routes: require('./common/routes'),
location: request.path,
onAbort: function (redirect) {
reply.redirect(redirect.to).permanent(true);
},
onError: function (err) {
console.log('Routing Error');
console.log(err);
}
});
router.run(function(Handler, state) {
var isNotFound = state.routes.some(route => route.name === 'not-found')
if (isNotFound) {
var html = React.renderToString(<Handler />);
reply.view('index', {
content: html
}).code(404);
return;
}
var html = React.renderToString(<Handler from='Server' />);
reply.view('index', {
content: html
});
});
I deployed an example as a show case of this issue: https://safe-ridge-9848.herokuapp.com/
When I click on a wrong url (Last link in the list below) it redirects to /fu and it shows not found, instead of rendering it using javascript (without loading the whole /fu page).
When you use <a href> instead of <Link>, it will reload the page (there are exception like hash links href="#elemid"). Some routers automatically hijack every <a> on the page; react-router does not, but gives you <Link> instead.
Try <li><Link to="/fu">Errr URL -> NotFound</Link></li> instead.
@taurose Yeah you are right!
I believe I tried it your solution before, but just found that to value should begins with / since fu is not in the Routes (I didn't read this in the docs).