Router

class Router[BasePage](routes: List[Route[_ <: BasePage, _]], serializePage: BasePage => String, deserializePage: String => BasePage, getPageTitle: BasePage => String, routeFallback: String => BasePage, deserializeFallback: Any => BasePage)(`$popStateEvent`: EventStream[PopStateEvent], owner: Owner, val origin: String, initialUrl: String)
Value Params
$popStateEvent
  • typically windowEvents.onPopState in Laminar
deserializeFallback
  • receives raw state from History API as input and returns a page to render when the entry in the history can not be deserialized using deserializePage. If you want to perform any logic (such as redirects) in case of this fallback, put it in the renderer (whatever you have responding to router.$currentPage).
deserializePage
  • parse the result of serializePage back into a page. This is called when user navigates using browser back / forward buttons and we load history state If you throw here, deserializeFallback will be called instead.
initialUrl
origin
owner
  • typically unsafeWindowOwner in Laminar (if the router should never die, i.e. it's a whole app router)
routeFallback
  • receives URL as input (either initialUrl or on hashChange event) and returns a page to render when none of the routes match on initial page load. When rendering a fallback page, the URL will stay the same. If you want to perform any logic (such as redirects) in case of this fallback, put it in the renderer (whatever you have responding to router.$currentPage).
routes
  • List of routes that this router can handle. You should only have one router for your app, so put all your routes here. There is no concept of subrouters, this is achieved by a combination of Page type hierarchy and nested rendering signals using the patterns described in README. If none of the routes match the provided URL upon initial page load, routeFallback will be used instead.
serializePage
  • encode page into a History API state record. Return any string here (JSON string is ok) This is called any time you trigger navigation to a page.
Throws
Exception
  • when initialUrl is not absolute or does not match origin
Companion
object
class Object
trait Matchable
class Any

Value members

Concrete methods

def absoluteUrlForPage(page: BasePage): String

Note: This does not consider fallback pages unless you provided routes for them.

Note: This does not consider fallback pages unless you provided routes for them.

Throws
Exception

when matching route is not found in router.

def forcePage(page: BasePage): Unit

Forces router.$currentPage to emit this page without updating the URL or touching the history records.

Forces router.$currentPage to emit this page without updating the URL or touching the history records.

Use this to display a full screen "not found" page when the route matched but is invalid for example because /user/123 refers to a non-existing user, something that you can't know during route matching.

Note, this will update document.title unless the provided page's title is empty. I think updating the title could affect the current record in the history API (not sure, you'd need to check if you care)

def pageForAbsoluteUrl(url: String): Option[BasePage]
Throws
Exception

when url is malformed

def pageForRelativeUrl(url: String): Option[BasePage]
Throws
Exception

when url is not relative or is malformed

def pushState(page: BasePage): Unit

Pushes state by creating a new history record. See History API docs on MDN.

Pushes state by creating a new history record. See History API docs on MDN.

def relativeUrlForPage(page: BasePage): String

Note: This does not consider fallback pages unless you provided routes for them.

Note: This does not consider fallback pages unless you provided routes for them.

Throws
Exception

when matching route is not found in router.

def replaceState(page: BasePage): Unit

Replaces state without creating a new history record. See History API docs on MDN.

Replaces state without creating a new history record. See History API docs on MDN.

Concrete fields

val `$currentPage`: StrictSignal[BasePage]

Note: this can be in an errored state if:

Note: this can be in an errored state if:

  • initialUrl does not match origin
  • routeFallback was invoked and threw an Exception
  • deserializeFallback was invoked and threw an Exception
val origin: String