Quickstart
This is the shortest path to a routekit app. By the end you’ll have a single-page app that routes between three pages, lazy-loads each, and is fully typed.
-
Install the package.
Terminal window npm install routekit -
Define your routes. A route is a
pathand ahandler. The handler is any async function — typically() => import(...)for code-splitting.src/routes.ts export const routes = [{ path: '/', handler: () => import('./pages/home') },{ path: '/posts', handler: () => import('./pages/posts') },{ path: '/posts/:id', handler: () => import('./pages/post') },] as const;The
as constis what gives you type-safe params — see TypeScript usage. -
Mount the router.
src/main.ts import { createRouter } from 'routekit';import { routes } from './routes';const router = createRouter(routes);router.on('navigate', async ({ match }) => {const { default: render } = await match.handler();render(document.querySelector('#app')!, match.params);});router.start(); -
Build a page handler. Each route module exports a default
renderfunction. routekit doesn’t care what’s inside — it only calls the function.src/pages/post.ts export default function render(root: HTMLElement, params: { id: string }) {root.innerHTML = `<h1>Post #${params.id}</h1>`;} -
Run it. Use any dev server (Vite, esbuild, Bun, plain
http-server). Navigate to/posts/42and the post page will render.
What just happened
The first time you visit /posts/42:
- The browser fires a navigation event when
router.start()readswindow.location. - routekit matches
/posts/42against the route table → finds/posts/:id→ extracts{ id: '42' }. - The match is delivered to your
'navigate'handler. - Your handler calls
match.handler()— which lazy-imports./pages/post— and renders into#app.
Everything after this is just iterating: more routes, nested layouts (see navigation), code-split chunks (see code splitting).
Next: your first route
Walk through building a real route from scratch, with type-safe params and an active-link styling helper.