Configure Nested Routes
mainRoutes can be nested using the children property. This allows for hierarchical URL structures.
- A child route with
path: ''matches the parent's path exactly. - Nested paths are relative to the parent's path.
- Catch-all behavior: Setting
children: [](an empty array) on a route makes it act as a catch-all for any URL starting with that route's path.
const router = new UniversalRouter({
path: '/admin',
children: [
{
path: '', // matches /admin
action: () => 'Admin Page',
},
{
path: '/users',
children: [
{
path: '', // matches /admin/users
action: () => 'User List',
},
{
path: '/:username', // matches /admin/users/john
action: () => 'User Profile',
},
],
},
],
})
router.resolve({ pathname: '/admin/users/john' }).then(console.log)
// => User Profile