lapis icon indicating copy to clipboard operation
lapis copied to clipboard

Route precedence with unnamed routes?

Open TangentFoxy opened this issue 6 years ago • 3 comments

In my application, I have the following routes:

[something: "/*"]: =>
"/submit": =>
"/ksp/*: =>

I would have thought trying to load "/ksp/craft/3" for example would go to the third route, but it gets captured by the named route instead. However, when I changed the third route to be named, it suddenly works as I expected.

How is route precedence affected by named vs unnamed routes? Also how is precedence affected when there are routes with splats in different locations (but same count of splats)? The documentation takes into account different counts, but doesn't say anything about this.

TangentFoxy avatar Mar 02 '18 06:03 TangentFoxy

The name of the route doesn't affect precedence, it just probably happened to change the order the routes were stored in an array somewhere. There is no precedence rule for things with same number of splats right now when there is overlap. Maybe the position of the * in the string can dictate what the precedence is compared to things with same number of splats? This kinda breaks down though when you have combinations with multiple splats, so maybe there's a better way to sort

leafo avatar Mar 02 '18 17:03 leafo

Maybe we can take literal length into consideration when calculating precedence, like:

https://github.com/tianchaijz/lua-resty-router/blob/master/lib/resty/router/router.lua#L230

PS: this project is awesome, I just extract a lua-resty module from the router module.

tianchaijz avatar Mar 15 '18 07:03 tianchaijz

This kinda breaks down though when you have combinations with multiple splats

Can you give an example of how it might break down?

Also, I just noticed the documentation does not express how a route with multiple splats will be handled, just implies that it is possible when discussing route precedence.

TangentFoxy avatar Mar 16 '18 00:03 TangentFoxy