Skip to content

matchPathPrefix() function

Home > @rimitive/router > matchPathPrefix

Matches a URL path against a route pattern (prefix match for parent routes)

Used for routes with children - matches if the path starts with the pattern

Signature:

matchPathPrefix: (pattern: string, path: string) => RouteMatch | null

Parameter

Type

Description

pattern

string

path

string

Returns:

RouteMatch | null

import { matchPathPrefix } from '@rimitive/router';
// Prefix match for parent routes
matchPathPrefix('/products', '/products/123');
// { path: '/products/123', params: {} }
// With parameters
matchPathPrefix('/blog/:year', '/blog/2024/hello-world');
// { path: '/blog/2024/hello-world', params: { year: '2024' } }
// Root matches everything
matchPathPrefix('/', '/any/path');
// { path: '/any/path', params: {} }
// Exact match still works
matchPathPrefix('/about', '/about');
// { path: '/about', params: {} }
// No match when path is shorter
matchPathPrefix('/products/category', '/products');
// null