Skip to main content

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 Location object with pathname, search, and hash
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 path
  • pathname - The matched portion of the URL
  • pathnameBase - The matched pathname before child routes
  • route - 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:

Notes

  • Returns null if no routes match (not an empty array)
  • Matches are ordered from parent to child (root first, leaf last)
  • The basename is stripped before matching but included in the returned pathnames
  • Dynamic segments (:param) are extracted into the params object
  • Optional segments (path?) and wildcards (*) are supported