Summary
Matches the given routes to a location and returns the match data. Useful for server-side rendering, testing, or determining which routes would match a given URL without actually navigating.Signature
Parameters
RouteObjectType[]
required
The array of route objects to match against. Each route can have
path, children, and other route properties.Partial<Location> | string
required
The location to match against. Can be:
- A string pathname (e.g.,
/dashboard) - A partial
Locationobject withpathname,search, andhash
string
default:"/"
Optional base path to strip from the location before matching. Useful when your app is served from a subdirectory.
Returns
AgnosticRouteMatch[] | null
An array of matched routes ordered from parent to child, or
null if no matches were found.Each match object contains:params- URL parameters extracted from the pathpathname- The matched portion of the URLpathnameBase- The matched pathname before child routesroute- The route object that matched
Examples
Basic usage
With URL parameters
With Location object
With basename
Checking for matches
Common Use Cases
Server-side rendering
Determine which routes match before rendering:Preloading route data
Access control checks
Extracting params without navigation
Testing route configuration
Building breadcrumbs
Route Matching Behavior
Nested routes
Child routes are only matched if their parent matches:Index routes
Wildcard routes
Performance Considerations
- Route matching is synchronous and fast
- Results can be cached if routes don’t change
- For large route trees, consider memoizing results
Type Safety
Use TypeScript generics for type-safe route matching:Related Functions
matchPath- Match a single path patternresolvePath- Resolve relative pathsgeneratePath- Generate paths from patterns
Notes
- Returns
nullif no routes match (not an empty array) - Matches are ordered from parent to child (root first, leaf last)
- The
basenameis stripped before matching but included in the returned pathnames - Dynamic segments (
:param) are extracted into theparamsobject - Optional segments (
path?) and wildcards (*) are supported