kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 104 forks source link

wild card route weird behavior #150

Closed AhmadMayo closed 6 years ago

AhmadMayo commented 6 years ago

I'm submitting a ...

Routes definition:

const routes = {
  path: '',
  children: [
// paths
  {
     path: '(.*)'
    action(context, params) {
      console.log(params);
    }
  ]
}

Going to the route / -which I don't have a defined route for - will log this to the console {0: '/'} The problem is that defining the path as (.*) will create an unnamed parameter with the value '/', and defining the path like this /(.*) will create an unnamed parameter with value '' (empty string). I use a generic action because I handle all my routes in the same way, so suddenly I find the url of my app looks like this '/?0=%2F' or like this '?0=' or worse - if I go to 'blah' the url will look like this '/?=%2Fblah'

I've read the path-to-regex documentation, and I found out that this is the desired behavior for the unnamed parameters (.*), but this is not the desired behavior for wild card path. Please provide another way to handle wild card paths, while keeping the same behavior for unnamed parameters, as they are not the same case.

Side note: I love your work, I have been searching for a universal router for about a year before I have found your package, and I have not used any other router since. Brilliant work, great flexibility. I will submit a PR soon to add more recipes.

frenzzy commented 6 years ago

You can use named param:

const route = {
  path: ':any(.*)',
  action: (context) => {
    console.log(context.params); // => { any: '/' }
  }
}

Demo: https://jsfiddle.net/b0w9mjck/114/

What the expected behavior you want to achieve?

AhmadMayo commented 6 years ago

no params at all. I don't want the router to treat url segments as params, I want him to treat the whole url as 'not found' or 'other'

frenzzy commented 6 years ago

Have you tried to use regexp path: /.*/,? https://jsfiddle.net/b0w9mjck/118/

AhmadMayo commented 6 years ago

works like a charm. Thank you.

AhmadMayo commented 6 years ago

This should be mentioned in the read me