All posts
Apr 12, 2026·7 min

Building a 3KB router for React

ReactTypeScriptOpen Source

Most apps reach for a routing library on day one and never think about it again. I wanted to understand what that library is actually doing, so I built 9Router — a sub-3KB client router.

The core is smaller than you’d expect: a subscription to the History API, a matcher that turns a path pattern into a regex, and a context that re-renders subscribers when the location changes.

The interesting part is types. By parsing the path string at the type level you can infer params, so `/users/:id` gives you a typed `{ id: string }` with zero runtime cost.

Lazy loading falls out naturally once routes are just objects: wrap the element in Suspense and let the bundler split it. The whole thing taught me more about routing than five years of useNavigate ever did.