5

My routes are defined as follows:

 <Router history={browserHistory}>
   <Route path="/" component={App}>
     <Route path="experiments">
       <IndexRoute component={Experiments} />
     </Route>
     <Route path="*" component={NoMatch}/>
   </Route>
 </Router>

When I visit /experiments, things work as expected and the Experiments component is rendered. However, when I manually enter a URL with query parameters, example: /experiments?offset=50, the route doesn't match!

But when I navigate using <Link to={{ pathname='/experiments', query={offset:50} }} />, things work as expected. The Experiments component is rendered and this.props.location.query.offset is set to 50.

How do I get the Route to match when a URL with query string is entered manually (or copy-pasted)?

It seems route should match automatically url-with-query-‌​string-not-matching-‌​react-router-route, but it doesn't seem to be working for me.

Edit: I narrowed down the problem to the catch-all route path="*". When I remove this route, everything works (e.g. when I visit /experiments?offset=50). But no routes are matched when the catch-all Route is present, even though it is at the bottom of the precedence list.

4
  • Please go back in and edit your post rather than adding on new information in a comment. Refer to: How to Ask and please show a minimal reproducible example next time. You also didn't even bother to complete the 2-minute site tour before asking. Commented Dec 22, 2016 at 2:45
  • 1
    Apologies. I edited my post to add the new info. Commented Dec 22, 2016 at 3:01
  • Excellent. I just up-voted you for doing that. One day maybe you'll be editing new user questions and telling them the same, or similar things. Commented Dec 22, 2016 at 3:03
  • I had a similar problem, but the match didn't work when it came from a Redirect. The error was on the redirect usage. I've posted my solution here stackoverflow.com/a/68852335/6928599. Just mentioning because the problem here was really similar to what I was having, so it can help others in the future. Commented Aug 19, 2021 at 17:47

2 Answers 2

0

You need to make use of historyApiFallback with history={browserHistory} to load your route when you manually enter it.

what historyApiFallback does, is make it so your server returns index.html for whatever URL you try to access and since your routes are then configured with respect to index.html so you can access any of the route url directly

In you webpack add

devServer: {
    historyApiFallback: true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using create-react-app and I believe that it uses historyApiFallback already.
0

Ok, turns out this was an entirely unrelated issue. I was using redux-auth which was causing a redirect to the wrong URL pathname. This was happening after the react-router did its parsing and route matching. Once I fixed that, route matching worked perfectly. I was able to parse the query string from a URL like /experiments?offset=50 using the Route config in my question above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.