React-router not rendering pages not found correctly

Asked

Viewed 720 times

1

I’m developing an application using React + React-router + Express to render to the server and injecting it into an EJS view.

It is working properly, but I noticed that when not finding a route as /naoexiste request falls correctly on React-router and my component PaginaNaoEncontrada is rendered correctly (client rendering), however, if a request like /naoexiste/ is done, my component is rendered in the server version (which has not loaded Bundle and some other things that are loaded into html, such as external styles).

Am I missing something ?

Routes.js

export default (
    <Route path="/" component={MainPage}>
        <IndexRoute component={Pagina}/>
        <Route path="pagina" component={Pagina}/>
        <Route path='*' component={PaginaNaoEncontrada} />
    </Route>
);

App.js

export default class App extends React.Component {
  render() {
    return (
        <Router history={browserHistory}
            routes={routes}
            onUpdate={()=> window.scrollTo(0,0)}
        />
    );
  }
}

Server.js (Part of the route control, there are other things but I don’t think they interfere in this issue)

app.get('*', function (req, res) {
  (0, _reactRouter.match)({ routes: _routes2.default, location: req.url }, function (err, redirectLocation, renderProps) {
    if (err) {
      return res.status(500).send(err.message);
    }

    if (redirectLocation) {
      return res.redirect(302, redirectLocation.pathName + redirectLocation.search);
    }

    var markup = void 0;
    if (renderProps) {
      markup = (0, _server.renderToString)(_react2.default.createElement(_reactRouter.RouterContext, renderProps));
    } else {
      markup = (0, _server.renderToString)(_react2.default.createElement(_NotFoundPage2.default, null));
      res.status(404);
    }

    return res.render('index', { markup: markup });
  });
});

Versions:

"react": "^15.4.1",
"react-dom": "15.4.1",
"react-router": "^3.0.0",
"ejs": "^2.5.3",
"express": "^4.12.4",
  • What version of React Router are you using?

  • @Sergio Editei with the versions

  • I think in the old versions you could do '*/?', you already tested that?

  • @Sergio had already tried with */ now I’ve done with */? but I had the same result :/

  • I find it strange that even existing routes happen this, if I declare the route as planos and type planos/

  • 1

    Put forehead <Route path='*' outside the component <Route path="/", something like in this example of documentation.

  • @Sergio Fiz and gave the same result, I’m trying to redirect the 404 calls on the server to "/pagina_nao_found" or something like that but nothing done tbm

  • @Sergio, I found out what it was, something really stupid even, by importing the Bundle was not putting the bar, for example : <script src="bundle.js"></script> instead of <script src="/bundle.js"></script> this caused the server to search the files in the path that was instead of the root :/

Show 3 more comments

1 answer

0

I was able to find where the error was.

TL;DR

By importing the bundle.jsthat contained client logic was not using /bundle.js and yes bundle.js

Thus, even though the server did the correct page pre-render the request to recover this Bundle failed because it tried to search the current address and not from the root.

Exemplifying:

By calling http://localhost:3000/como_funciona the server gave me the correct component and the request for Bundle happened from the root "/bundle.js", however, by calling http://localhost:3000/como_funciona/ the server rendered the correct page, but the call to Bundle happened at the address http://localhost:3000/como_funciona/bundle.js so it was not found.

An addendum that I realized too, is that this happened when using a <Link to="pagina" /> instead of <Link to"/pagina" /> if it is in the root goes quiet, otherwise it will concatenate with the current address.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.