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.

Routing with wildcard paths is impossible

See original GitHub issue

I’ve been using this router for a week and finally ran into my first major issue that’s so far been impossible to work around. The code in master would seemingly allow a workaround, but the code deployed to NPM does not.

As a bit of back story, I am using redux-first-router as a top-level router for my app, and everything but the first level is handled by other child apps with their own routers. I am using wildcards in my route configuration like this:

const routesMap = {
  ROUTE_APP_A: {
    path: '/appA(.*)',
    ...,
  },
  ROUTE_APP_B: {
    path: '/appB(.*)',
    ...,
  }
}

That configuration works fine for most cases except for when I dispatch an action which includes paths that to take the place of the wildcard. This is an example of the action I’m dispatching:

dispatch({
  type: 'ROUTE_APP_A',
  payload: { 0: '/subRoute' }
});

The router will run through this code in pure-utils/actionToPath.js with each item in the payload:

var transformSegment = function transformSegment(segment, route, key) {
  if (typeof segment === 'string') {
    if (segment.indexOf('/') > -1) {
      return segment.split('/');
    }

    if (route.capitalizedWords === true) {
      return segment.replace(/ /g, '-').toLowerCase();
    }

    if (typeof route.toPath === 'function') {
      return route.toPath(segment, key);
    }

    return segment;
  } else if (typeof segment === 'number') {
    return segment;
  }
};

It hits the first check segment.indexOf('/') > -1, and splits the string to an array of ['', 'subRoute'].

Eventually, that param gets to path-to-regexp which throws the following error:

TypeError: Expected "0" to not repeat, but received `["","subRoute"]`

The error is swallowed by redux-first-router here and then it routes to the not-found page.

The code that is currently in master could provide a workaround since route.toPath is called, but that’s still not a clean solution.

For reference, I’m using 0.0.16-next which is the current “latest” tag on NPM.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stezucommented, Apr 15, 2018

Thanks @faceyspacey, that worked! Now my routesMap looks like this:

const routesMap = {
  ROUTE_APP_A: {
    path: '/appA/:subRoute*',
    ...,
  },
  ROUTE_APP_B: {
    path: '/appB/:subRoute*',
    ...,
  }
};

And I dispatch actions like this:

dispatch({
  type: 'ROUTE_APP_A',
  payload: {
    subRoute: 'routeA/routeB/routeC'
  }
});

Just documenting in case someone else lands on this issue in the future.

1reaction
faceyspaceycommented, Apr 15, 2018

does this work (taking from tests from the upcoming version, which should still work here):

createTest('optionally match "multi segment params" as single param', {
  REPO1: {
    path: '/repo1/:user/:repo/blob/:branch/:filePath*'
  },
  REPO2: {
    path: '/repo2/:user/:repo/blob/:branch/:filePath*'
  }
}, [
  {
    type: 'REPO1',
    params: {
      user: 'faceyspacey',
      repo: 'rudy',
      branch: 'master'
    }
  },

:filePath can be a url to a file like on a github repo /foo/bar/bla – and its named

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Routing with wildcards not always working
Using Angular 4, I have the following in my Routes collection: { path:"**", //wildcard component: NotFoundComponent, }.
Read more >
Wildcard Routes (**) Can Redirect Relative To Their UrlTree ...
As a quick follow-up post, I wanted to demonstrate that a wildcard route can redirect to a URL that is relative to its...
Read more >
Router in axum::routing - Rust - Docs.rs
Add another route to the router. path is a string of path segments separated by / . Each segment can be either static,...
Read more >
Laravel 6 Tutorial for Beginners #10 - YouTube
Hey gang, in this Laravel tutorial we'll take a look at route parameters, otherwise known as wildcards. ‍ ‍...
Read more >
What are Route Pattern Wildcards? - YouTube
Start learning cybersecurity with CBT Nuggets. https://courses.cbt.gg/securityIn this video, CBT Nuggets trainer Lalo Nunez covers route ...
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