Skip to content

nexa-router

Client-side router with path matching, navigation, and guard support.

createRouter

Creates a router instance with a set of routes.

ts
import { createRouter } from 'nexa-router'

interface RouteDefinition {
  path: string
  component: Component
  guards?: RouteGuard[]
}

interface RouterConfig {
  routes: RouteDefinition[]
  guards?: GuardRegistry
}

interface Router {
  (): VNode
  start(): void
}

function createRouter(config: RouterConfig): Router

The router itself is a component — pass it to mount:

ts
const router = createRouter({ routes: [...] })
mount('#app', router)

Navigates to a path or moves through history.

ts
import { navigate } from 'nexa-router'

function navigate(to: string | number): void
ArgumentBehavior
'/path'Navigates to the given path
-1Goes back one history entry
1Goes forward one history entry

currentPath

A signal containing the current URL pathname.

ts
import { currentPath } from 'nexa-router'

const path: Signal<string>

Example:

ts
effect(() => console.log(currentPath.value))

currentRoute

A signal containing the matched route definition and extracted parameters.

ts
import { currentRoute } from 'nexa-router'

interface MatchedRoute {
  path: string
  component: Component
  params: Record<string, string>
}

const currentRoute: Signal<MatchedRoute | null>

Example:

ts
effect(() => {
  if (currentRoute.value) {
    console.log('Route:', currentRoute.value.path)
    console.log('Params:', currentRoute.value.params)
  }
})

A component that renders an anchor tag with client-side navigation.

ts
import { Link } from 'nexa-router'

function Link(props: { to: string; children?: any; class?: string }): VNode

Example:

tsx
// In template:
<Link to="/about">About</Link>

createGuards

Creates a guard registry for route protection.

ts
import { createGuards } from 'nexa-router'

interface GuardContext {
  path: string
  params: Record<string, string>
}

type GuardFn = (to: GuardContext, from: GuardContext | null) => boolean | Promise<boolean>

interface GuardRegistry {
  beforeEach: GuardFn[]
}

function createGuards(guards: {
  beforeEach?: GuardFn | GuardFn[]
}): GuardRegistry

Example:

ts
const guards = createGuards({
  beforeEach: (to, from) => {
    if (to.path.startsWith('/admin') && !isLoggedIn) return false
    return true
  },
})

runGuards

Executes a chain of guards against a route transition.

ts
import { runGuards } from 'nexa-router'

function runGuards(
  guards: GuardRegistry,
  to: GuardContext,
  from: GuardContext | null
): Promise<boolean>

matchPath

Matches a path string against a route pattern with :param support.

ts
import { matchPath } from 'nexa-router'

function matchPath(
  pattern: string,
  path: string
): Record<string, string> | null

Returns a params object on match, or null if no match.

Examples:

ts
matchPath('/users/:id', '/users/42')
// Returns: { id: '42' }

matchPath('/users/:id', '/posts/1')
// Returns: null

Released under the MIT License.