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.

Param matchers greedy match defined routes

See original GitHub issue
const routes = [
  { name: 'home', path: '/' },
  { name: 'home.moo', path: 'moo' },

  { name: 'eviltroll', path: '/:catch' } 
]

// Expect to match "home.moo"
router.matchPath('/moo')
// -> { name: 'eviltroll', params: { catch: 'moo' } ...

😦

I’m probably doing something wrong here, because this seems like quite an obvious use-case, defining one set of handlers for matching routes, and letting mis-hits continue through to the rest of the defined routes. What I didn’t expect was for the ‘param’ operator (eviltroll) to be greedy and match the route defined by the “home.moo” handler.

Is this expected behaviour? And if so, can the intended match above be reached some other way?

Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
cayuucommented, Jul 22, 2018

Thanks Thomas, understood. Will design for this consideration. #closing

0reactions
trochcommented, Jul 22, 2018

To clarify: when two path definitions like /moo and /:param are siblings (they are children of the same parent node), /moo is tried before /:param.

The reason for not returning all matches and then picking the “best” one (i.e. the most specific one - with less params) is a design decision for increased performance and scalability: especially in the scenario of server-side rendering. That kind of match strategy is common server-side: the first match is the match.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Route Matching - Tutorial
A route has three attributes that determine whether or not it “matches” the URL: ... characters (greedy) until the next /, ?, or...
Read more >
Broadcast channel routes are very greedy when matching ...
Broadcast channel routes are very greedy when matching parameter routes # ... While this sounds like it still matches the route definition, ...
Read more >
Routing to controller actions in ASP.NET Core
ASP.NET Core controllers use the Routing middleware to match the URLs of incoming requests and map them to actions. Route templates:.
Read more >
Routing Configuration - Documentation - SAPUI5 SDK
Each route defines a name, a pattern, and optionally one or more targets to which to navigate when the route has been matched....
Read more >
What does the Express route param regex `(*)` mean?
I understand that the path is split on the slashes, so I thought I could use a regular expression in parentheses after the...
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