Routes
Routes are JavaScript objects that describe valid locations within an application.
Route Properties
Routes have two required properties—name
and path
—and a number of optional properties.
Route path
A route's path
is used to determine if a route matches a location. Path strings use path-to-regexp
formatting, which allows you to define dynamic path parameters that a route should match.
Route names
A route's name
is a unique identifier for a route.
The uniqueness of names is important so that you can interact with routes. The router's route
method is used for getting data about a route using its name. Curi also has a concept of route interactions for working with this route data. For example, Curi provides a pathname
route interaction to generate the pathname of a URL. This is particularly useful for routes with path params, since the pathname
interaction will automatically insert these for you.
Asynchronous Routes
When a route matches, you might want to perform some actions before the application re-renders. This can include validating that a user is authorized to navigate to a route and loading data based on the path parameters parsed from the location.
A route's resolve
property is a function that runs asynchronous actions and returns a Promise. A response will not be emitted until after a route's resolve
function has resolved.
A route with resolve
properties is asynchronous, which has some effects to be aware of. You can read about these in the Sync or Async guide.
The Response Function
Each route can have a respond
function. When a route matches, a response object with "match" properties is generated. An object returned by the respond
function gets merged with the match response object*. The responses guide covers all of the response properties.
* Only valid properties are merged.
The respond
function receives an object with a number of properties.
match
is an object of properties based on the matched route.resolved
is an object with the results of the route'sresolve
functions.error
is only defined when theresolve
function has an uncaught error.
Matching Routes
When you create a router, you pass an array of all of the valid routes for the application. Whenever Curi receives a new location, it matches the new location's pathname
against the valid routes to determine which one matches.
Route matching tests the route objects in the order that they are defined in the array. If a route partially matches (i.e. it matches the beginning of the pathname, but there are leftover unmatched segments of the pathname), and it has children
routes, those will be checked before moving to the route's next sibling.
No Matching Route
If none of your routes match a location, Curi will do nothing! Your routes should include a "catch all" route to match these locations. The path "(.*)"
matches every pathname, so using that as the path of the last route will catch every location.
Route Matching Walkthrough
If the user navigates to a location with the pathname "/a/red/yellow"
, the Album
route will only partially match, so Curi will move on to the next route, Not Found
. Not Found
has a catch all path
that matches every pathname, so it will match the location.
If a route has children, Curi will check if any of those routes form a complete match before moving on to the next route in the routes array.
If the pathname is '/a/Coloring+Book/All+Night'
, the Album
route will partially match the pathname ("/a/Coloring+Book"
). That route has children routes, so the unmatched segments ("/All+Night"
) will be checked against those routes. The Song
route matches the remaining segments, so the router matches the Song
route to the location.
Path Matching Options
You can control whether a route does exact or partial matching with pathOptions
property. If you set { end: false }
, a route that partially matches will consider itself matched.
Preparing Routes
The routes array should be wrapped in a prepareRoutes
call. This will pre-build the routes for the router, which is especially useful for server rendering, where a new router is created for every request.