raquo / Waypoint

Efficient router for Laminar UI Library
MIT License
92 stars 11 forks source link

router.pageForAbsoluteUrl seems not working correctly #21

Closed NPCRUS closed 1 month ago

NPCRUS commented 2 months ago

I have a following very simple router setup:

object Router {

  sealed trait Page
  object Page {
    case object Main extends Page
    case object Auth extends Page
    case object IonicTest extends Page
    case object NotificationTest extends Page
    case object RpcTest extends Page
  }
  import Page.*

  private val mainRoute = Route.static(Main, root)
  private val authRoute = Route.static(Auth, root / "auth")
  private val ionicTestRoute = Route.static(IonicTest, root / "ionicTest")
  private val notificationTest =
    Route.static(NotificationTest, root / "notificationTest")
  private val rpcTestRoute =
    Route.static(RpcTest, root / "rpcTest")

  val router = new Router[Page](
    routes = List(mainRoute, authRoute, ionicTestRoute, notificationTest, rpcTestRoute),
    getPageTitle = _.toString,
    serializePage = _.toString,
    deserializePage = {
      case "Main" => Main
      case "Auth" => Auth
      case "IonicTest" => IonicTest
      case "NotificationTest" => NotificationTest
      case "RpcTest" => RpcTest
    }
  )(
    popStateEvents = L.windowEvents(_.onPopState),
    owner = L.unsafeWindowOwner
  )
}

after initialization I have a simple printline:

println(router.pageForAbsoluteUrl(router.absoluteUrlForPage(Page.Auth)))

which returns Some(Main) Am I doing something wrong or is it a bug?

raquo commented 2 months ago

You're missing / endOfSegments at the end of your route patterns (should be root / endOfSegments, root / "auth" / endOfSegments, etc.), so the first route pattern, root, ends up matching all URLs under /, including "/auth".

I know, this is not exactly ergonomic. The incorrect pattern looks like what you'd expect the correct pattern could look like.

I've been wanting to change the default behaviour so that you wouldn't need / endOfSegments to match the path segments exactly, but could use / restSegments if you wanted to match and capture the remaining path segments. Haven't had the time, so far. This needs a change in URL-DSL.

NPCRUS commented 1 month ago

Thanks for clarification, it's a miss on my side then. Thank you!