new FlexRoute(options)
Creates a route engine with mode, base path, route definitions, and lifecycle hooks.
The ultra-fast, zero-dependency conditional routing engine for modern JavaScript applications.
Describe route behavior as compact rules that stay readable as your app grows.
Dispatch loaders, metadata, and transitions through one predictable execution graph.
Use it with vanilla JavaScript, React, Vue, Svelte, or any custom renderer.
Installation
FlexRoute.js ships as a tiny ESM package with full TypeScript definitions and no required runtime adapter.
$ npm install flexroute
$ pnpm add flexroute
Quick Start
Define routes as plain objects, attach data loaders, and let the engine resolve each navigation with deterministic priority.
import { FlexRoute } from 'flexroute';
const router = new FlexRoute({
mode: 'history',
base: '/',
routes: [
{
path: '/',
name: 'dashboard',
load: async () => ({
title: 'Overview',
metrics: await fetchMetrics()
})
},
{
path: '/projects/:id',
name: 'project.detail',
match: ({ params }) => Boolean(params.id),
load: ({ params }) => fetchProject(params.id)
}
]
});
router.start(document.querySelector('#app'));
API Reference
new FlexRoute(options)
Creates a route engine with mode, base path, route definitions, and lifecycle hooks.
router.navigate(target)
Moves to a named route, absolute path, or structured location object.
router.current()
Returns the current route state, resolved params, query values, and loaded data.
router.on(event, handler)
Subscribes to lifecycle events such as beforeLoad, afterLoad, and error.
Advanced Settings
Advanced options help teams balance latency, cache strategy, and error recovery across complex application surfaces.
const router = new FlexRoute({
prefetch: 'intent',
cache: {
strategy: 'stale-while-refresh',
maxAge: 30_000
},
transitions: {
timeout: 250,
restoreScroll: true
}
});
FAQ
No. The core package is renderer agnostic and works with any component model or vanilla DOM rendering.
Yes. Loaders may return plain values or promises, and resolved data is attached to the current route state.
Yes. Route names, params, loader output, and event payloads are designed for strong editor inference.