File-based SPA router for Zenith framework with deterministic, compile-time route resolution
npm install @zenithbuild/routerFile-based SPA router for Zenith framework with deterministic, compile-time route resolution.
- š File-based routing ā Pages in pages/ directory become routes automatically
- ā” Compile-time resolution ā Route manifest generated at build time, not runtime
- š ZenLink component ā Declarative navigation with prefetching
- š§ Programmatic navigation ā navigate(), prefetch(), isActive() APIs
- šÆ Type-safe ā Full TypeScript support with route parameter inference
- š Hydration-safe ā No runtime hacks, works seamlessly with SSR/SSG
``bash`
bun add @zenithbuild/router
`ts
import { navigate, prefetch, isActive, getRoute } from '@zenithbuild/router'
// Navigate to a route
navigate('/about')
// Navigate with replace (no history entry)
navigate('/dashboard', { replace: true })
// Prefetch a route for faster navigation
prefetch('/blog')
// Check if a route is active
if (isActive('/blog')) {
console.log('Currently on blog section')
}
// Get current route state
const { path, params, query } = getRoute()
`
`html
`
The router generates a route manifest at compile time:
`ts
import { generateRouteManifest, discoverPages } from '@zenithbuild/router/manifest'
const pagesDir = './src/pages'
const manifest = generateRouteManifest(pagesDir)
// manifest contains:
// - path: Route pattern (e.g., /blog/:id)
// - regex: Compiled RegExp for matching
// - paramNames: Dynamic segment names
// - score: Priority for deterministic matching
`
| File Path | Route Pattern |
|-----------|---------------|
| pages/index.zen | / |pages/about.zen
| | /about |pages/blog/index.zen
| | /blog |pages/blog/[id].zen
| | /blog/:id |pages/posts/[...slug].zen
| | /posts/*slug |pages/[[...all]].zen
| | /*all? (optional) |
``
@zenithbuild/router
āāā src/
ā āāā index.ts # Main exports
ā āāā types.ts # Core types
ā āāā manifest.ts # Build-time manifest generation
ā āāā runtime.ts # Client-side SPA router
ā āāā navigation/
ā āāā index.ts # Navigation exports
ā āāā zen-link.ts # Navigation API
ā āāā ZenLink.zen # Declarative component
- navigate(path, options?) ā Navigate to a pathprefetch(path)
- ā Prefetch a route for faster navigationisActive(path, exact?)
- ā Check if path is currently activegetRoute()
- ā Get current route stateback()
- , forward(), go(delta) ā History navigation
- discoverPages(pagesDir) ā Find all .zen files in pages directorygenerateRouteManifest(pagesDir)
- ā Generate complete route manifestfilePathToRoutePath(filePath, pagesDir)
- ā Convert file path to routeroutePathToRegex(routePath)
- ā Compile route to RegExp
- RouteState ā Current route state (path, params, query)RouteRecord
- ā Compiled route definitionNavigateOptions
- ā Options for navigationZenLinkProps` ā Props for ZenLink component
-
MIT