@zenithbuild/router
v1.0.3
Published
File-based SPA router for Zenith framework with deterministic, compile-time route resolution
Maintainers
Readme
@zenithbuild/router
File-based SPA router for Zenith framework with deterministic, compile-time route resolution.
Features
- 📁 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
Installation
bun add @zenithbuild/routerUsage
Programmatic Navigation
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()ZenLink Component (in .zen files)
<ZenLink href="/about">About Us</ZenLink>
<!-- With prefetching on hover -->
<ZenLink href="/blog" preload>Blog</ZenLink>
<!-- External links automatically open in new tab -->
<ZenLink href="https://github.com">GitHub</ZenLink>Build-time Route Manifest
The router generates a route manifest at compile time:
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 matchingRoute Patterns
| 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) |
Architecture
@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 componentAPI Reference
Navigation Functions
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
Manifest Generation
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
Types
RouteState— Current route state (path, params, query)RouteRecord— Compiled route definitionNavigateOptions— Options for navigationZenLinkProps— Props for ZenLink component
License
MIT
