smolter
API

matchRoutes

Find the first route whose pattern matches a pathname.

Compatibility

ReactAvailable in this renderer's browser integration.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactAvailable in this renderer's browser integration.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/utils

Signature

function matchRoutes(
  routes: readonly Route[],
  pathname: string,
): Match | undefined;

Parameters

routes is ordered. pathname is only the URL pathname, excluding query and hash.

Returns

{ route, params } or undefined.

Usage

Import this API from one of the entry points listed above. Keep server and build helpers in their dedicated subpaths.

const match = matchRoutes(routes, new URL(request.url).pathname);
console.log(match?.params.id);

Behavior and edge cases

Matching is case-sensitive. One trailing slash is ignored. Supports static segments, :parameters, and terminal *. Invalid percent sequences are decoded using URLSearchParams replacement behavior rather than throwing. Static routes must precede dynamic ones in a manual table.

Repeated matching

Parsed path patterns are reused in a bounded cache of up to 256 entries. Route order and current modules still determine each match; params and loader results are never cached here. Replacing a route object or changing its path remains supported. Eviction only requires parsing the pattern again.

View implementation ↗