diff --git a/.changeset/path-matcher-segment-wildcard.md b/.changeset/path-matcher-segment-wildcard.md new file mode 100644 index 00000000000..7ad08ff33cd --- /dev/null +++ b/.changeset/path-matcher-segment-wildcard.md @@ -0,0 +1,5 @@ +--- +'@clerk/shared': patch +--- + +`createRouteMatcher()` route suggestions now use the `:path*` subtree form (e.g. `/dashboard/:path*`) instead of `(.*)`. Unlike `/dashboard(.*)`, which also matches sibling routes such as `/dashboardxyz`, `/dashboard/:path*` matches only `/dashboard` and its path-segment subtree. Existing `(.*)` patterns keep working; this only changes the type-level autocomplete suggestion. diff --git a/packages/shared/src/pathMatcher.ts b/packages/shared/src/pathMatcher.ts index d5e6e49f516..4ee5ccf42fc 100644 --- a/packages/shared/src/pathMatcher.ts +++ b/packages/shared/src/pathMatcher.ts @@ -1,7 +1,9 @@ import { pathToRegexp } from './pathToRegexp'; import type { Autocomplete } from './types'; -export type WithPathPatternWildcard = `${T & string}(.*)`; +// Suggests the `:path*` subtree form (e.g. `/dashboard/:path*`), which matches on +// path-segment boundaries. `/` is special-cased to `/:path*` to avoid a malformed `//:path*`. +export type WithPathPatternWildcard = T extends '/' ? '/:path*' : `${T & string}/:path*`; export type PathPattern = Autocomplete; export type PathMatcherParam = Array | RegExp | PathPattern;