Radix3 Router

1.1.2 · active · verified Sun Apr 19

Radix3 is a lightweight and high-performance routing library for JavaScript and TypeScript, currently stable at version 1.1.2. It implements a Radix Tree data structure to offer efficient route matching, making it particularly well-suited for server-side applications, API gateways, and edge runtimes where request routing speed is critical. The library maintains an active development cadence, with regular minor releases introducing enhancements and performance improvements, as seen in the recent updates to v0.8.x and v1.x. Its key differentiators include its minimal footprint, speed, and robust support for various route patterns like named parameters, wildcards, and regex-like segments. It also provides utilities for exporting and rehydrating route matchers, enabling pre-compiled routing logic for faster startup times. Radix3 ships with full TypeScript type definitions, ensuring a strong developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a Radix3 router, inserting various route patterns (static, named, wildcard), performing lookups, and utilizing the `toRouteMatcher` and `exportMatcher` utilities for advanced matching and persistence.

import { createRouter, toRouteMatcher, exportMatcher, createMatcherFromExport } from 'radix3';

// 1. Create a router instance and add routes
const router = createRouter({
  strictTrailingSlash: false,
  routes: {
    '/': { name: 'home' },
    '/users': { name: 'users-list' },
    '/users/:id': { name: 'user-detail' },
    '/assets/**': { name: 'static-assets' },
    '/docs/:version/**': { name: 'docs-wildcard' }
  }
});

// 2. Lookup routes
console.log('Matching /users:', router.lookup('/users'));
// Expected: { name: 'users-list' }

console.log('Matching /users/123:', router.lookup('/users/123'));
// Expected: { name: 'user-detail', params: { id: '123' } }

console.log('Matching /assets/img/logo.png:', router.lookup('/assets/img/logo.png'));
// Expected: { name: 'static-assets' }

// 3. Create a multi-matcher to find all matching routes
const matcher = toRouteMatcher(router);
const allMatches = matcher.matchAll('/docs/v1/introduction');
console.log('All matches for /docs/v1/introduction:', allMatches.map(m => m.name));
// Expected: ['docs-wildcard']

// 4. Export and rehydrate matcher for persistence/pre-compilation
const exportedMatcher = exportMatcher(matcher);
// In a real scenario, `exportedMatcher` would be serialized (e.g., to JSON) and loaded later.
const rehydratedMatcher = createMatcherFromExport(exportedMatcher);
console.log('Rehydrated matcher matching /users:', rehydratedMatcher.matchAll('/users').map(m => m.name));
// Expected: ['users-list']

view raw JSON →