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

How does universal router handle conflicts? #160

Open johnnash03 opened 5 years ago

johnnash03 commented 5 years ago

How universal-router handles the following conflicting routes:

  1. /tickets/:filghtName-:flightNumber
  2. /tickets/seattle-washington
frenzzy commented 5 years ago

From API documentation: router.resolve() traverses the list of routes in the order they are defined until it finds the first route that matches provided URL path string and whose action function returns anything other than null or undefined.

I.e. the order of the routes matters and you probably need to specify the most specific routes first:

const routes = [
  { path: '/tickets/seattle-washington', action: () => 1 },
  { path: '/tickets/:filghtName-:flightNumber', action: () => 2 },
]
const router = new UniversalRouter(routes)
router.resolve('/tickets/seattle-washington') // => 1
router.resolve('/tickets/washington-seattle') // => 2