Route Registration APIs

These utilities allow you to manage registered route patterns and compile parameterized paths dynamically.


registerRoute

Manually registers a route pattern with the central matching engine. This is performed automatically by <page-route> tags but can be done imperatively in JS.

typescript
1interface RegisterRouteOptions {2    exact?: boolean3    meta?: Record<string, unknown>4    name?: string5}6 7function registerRoute(8    pattern: string,9    options?: RegisterRouteOptions | boolean10): void

Example

javascript
1import { registerRoute } from '@beforesemicolon/router'2 3registerRoute('/dashboard')4registerRoute('/admin', { exact: false })5registerRoute('/users/:userId', {6    exact: true,7    name: 'user-routes',8    meta: { title: 'User Details', requiresAuth: true },9})

isRegisteredRoute

Checks whether a concrete pathname matches any registered route pattern.

typescript
1function isRegisteredRoute(pathname: string): boolean

Example

javascript
1import { isRegisteredRoute } from '@beforesemicolon/router'2 3isRegisteredRoute('/dashboard') // true4isRegisteredRoute('/users/42') // true, when /users/:userId is registered5isRegisteredRoute('/invalid-path') // false

parsePathname

Takes a path pattern containing parameters and compiles it into a full URL path using values from the current active location.

typescript
1function parsePathname(pattern: string): string

Example

javascript
1// Browser location is: /users/422// Current parameters: { userId: '42' }3 4import { parsePathname } from '@beforesemicolon/router'5 6const path = parsePathname('/users/:userId/details')7// Returns: "/users/42/details"
edit this doc