Skip to content

Function: findRouteByPath()

API / @xmachines/play-router / index / findRouteByPath

function findRouteByPath(tree, path, options?): RouteNode | undefined;

Defined in: packages/play-router/src/find-route.ts:96

Finds a route node by its URL path

The function looks the route node up by the URL path. It gives you the state ID of a browser URL, for the play.route event of a navigation.

When more than one state holds the same path, for example the root and a second state both at ”/”, the function prefers a node with a route, which means a node with a meta.route field, over a node without one.

The function also matches a pattern of a dynamic route, for example ‘/settings/:section?’.

Parameters

ParameterTypeDescription
treeRouteTreeThe route tree, from extractMachineRoutes
pathstringThe URL path, for example ‘/dashboard’ or ‘/settings/profile’
options?CompileOptionsThe caches that the pattern match reads. The default is the pair that @xmachines/play-url shares with every caller of the process. A server that serves many tenants passes a cache that the tenant owns, exactly as RouteMap and a bridge take one. A cache changes how often the work runs, and it changes no answer, so this argument is safe to leave out.

Returns

RouteNode | undefined

The route node, or undefined when the function finds none

Example

const tree = extractMachineRoutes(machine);
const node = findRouteByPath(tree, "/dashboard");
// With a cache that one request owns and drops
const node = findRouteByPath(tree, "/dashboard", {
patternCache: createLruCache(),
compiledCache: createLruCache(),
});