Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchPath not working with queryParams in url #5285

Closed
chrisgreg opened this issue Jun 26, 2017 · 7 comments
Closed

matchPath not working with queryParams in url #5285

chrisgreg opened this issue Jun 26, 2017 · 7 comments

Comments

@chrisgreg
Copy link

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 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) 
// [ ]
@timdorr
Copy link
Member

timdorr commented Jun 26, 2017

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

@timdorr timdorr closed this as completed Jun 26, 2017
@chrisgreg
Copy link
Author

chrisgreg commented Jun 26, 2017

@timdorr But then how do I get queryParams out of it inside my routes loadData function if the path is just the url without the queryParams?

EDIT: Clarity - props doesn't contain queryParams so I can't get them inside the Routes loadData call in.

@timdorr
Copy link
Member

timdorr commented Jun 26, 2017

You'll need to get the search into the returned match's location. Then you can parse it with whatever query parsing library you want.

@chrisgreg
Copy link
Author

@timdorr Can you point me into the right direction to do that?

@timdorr
Copy link
Member

timdorr commented Jun 26, 2017

Sorry, I misspoke. match doesn't have a location. Just pass in the request as a second arg to your loadData function. Or just request.query. Or whatever you want/need to successfully load data in there.

@remix-run remix-run deleted a comment from thomassuckow Jul 25, 2017
@liuhelen10
Copy link
Contributor

@timdorr is it possible to update the docs so that it specifies that req.path should be passed in rather than url? right now it still says to pass in req.url in two places:

https://reacttraining.com/react-router/web/guides/server-rendering
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/matchPath.md#pathname

@timdorr
Copy link
Member

timdorr commented Jan 16, 2018

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

liuhelen10 added a commit to liuhelen10/react-router that referenced this issue Jan 16, 2018
URL's with query strings don't get matched correctly when using `req.url`. `req.path` is required instead.

remix-run#5285
timdorr pushed a commit that referenced this issue Jan 16, 2018
URL's with query strings don't get matched correctly when using `req.url`. `req.path` is required instead.

#5285
jeresig pushed a commit to Khan/react-router that referenced this issue Aug 29, 2018
URL's with query strings don't get matched correctly when using `req.url`. `req.path` is required instead.

remix-run#5285
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants