How can react-router
properly handle 404 pages for dynamic content in a Universal app?
Let's say I want to display a user page with a route like '/user/:userId'
. I would have a config like this:
<Route path="/">
<Route path="user/:userId" component={UserPage} />
<Route path="*" component={NotFound} status={404} />
</Route>
If I request /user/valid-user-id
, I get the user page.
If I request /foo
, I get a proper 404.
But what if I request /user/invalid-user-id
. When fetching the data for the user, I will realize that this user does not exist. So, the correct thing to do seams to be:
- Display the 404 page
- Return a 404 http code (for server side rendering)
- Keep the url as is (I don't want a redirect)
How do I do that?? It seams like a very standard behaviour. I'm surprised not to find any example...
Edit:
Seams like I'm not the only one to struggle with it. Something like this would help a lot: https://github.com/ReactTraining/react-router/pull/3098
As my app won't go live any time soon, I decided to wait to see what the next react-router version has to offer...