question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

matchPath not working with queryParams in url

See original GitHub issue

So after reading https://github.com/ReactTraining/react-router/issues/4410 it looks like you can’t parse query params just by using react-router-v4.

I now have an issue that means my server-side data fetching doesn’t work (after following https://reacttraining.com/react-router/web/guides/server-rendering) because the matchPath function doesn’t match if you supply query params in the request?

Is there a solution for this?

Or have I simply encountered a bug relating to matchPath?

Example:


const routes = [
    {
        path: '/:locale/plp/.*',
        component: PLPContainer,
        loadData: props => {
          return getData()
        },
        key: 'PLP'
    },
    ...
];

export function collectServerSideCalls(routes, request) {
    const promises = [];
    routes.some(route => {
        const match = matchPath(request.url, route);
        if (match && route.loadData) {
            promises.push(route.loadData(match));
        }
        return match;
    });
    return promises;
}

// When req.url = `/en-gb/plp`
const promises = collectServerSideCalls(routes, req) 
// [ [Function] ]


// When req.url = `/en-gb/plp?fun=123`
const promises = collectServerSideCalls(routes, req) 
// [ ]


Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
timdorrcommented, Jun 26, 2017

That’s a typo. You should be putting request.path into matchPath (hence the name…)

0reactions
timdorrcommented, Jan 16, 2018

A PR would be the best way to get that fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

matchPath not working with queryParams in url #5285 - GitHub
So after reading #4410 it looks like you can't parse query params just by using react-router-v4. I now have an issue that means...
Read more >
React Router Match Path issue - Stack Overflow
matchPath is meant to receive only the path without a query string. ... of that here: matchPath not working with queryParams in url...
Read more >
How to handle query params in React Router
In this part, we will learn how to handle query params in the URL. Lets create a new route for search page with...
Read more >
matchPath - React Router v5
matchPath. This lets you use the same matching code that <Route> uses except outside of the normal render cycle, like gathering up data...
Read more >
How to use the react-router.matchPath function in react-router
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found