Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/path-matcher-segment-wildcard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/shared': patch
'@clerk/nextjs': patch
---

`createPathMatcher()` and `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. The new suggestion type is exported as `WithPathSegmentWildcard`; the existing `WithPathPatternWildcard` type is unchanged (now deprecated), and `(.*)` patterns keep working. This only changes the type-level autocomplete suggestion.
4 changes: 2 additions & 2 deletions packages/nextjs/src/server/routeMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createPathMatcher, type WithPathPatternWildcard } from '@clerk/shared/pathMatcher';
import { createPathMatcher, type WithPathSegmentWildcard } from '@clerk/shared/pathMatcher';
import type { Autocomplete } from '@clerk/shared/types';
import type Link from 'next/link';
import type { NextRequest } from 'next/server';

type NextTypedRoute<T = Parameters<typeof Link>['0']['href']> = T extends string ? T : never;
type RouteMatcherWithNextTypedRoutes = Autocomplete<WithPathPatternWildcard<NextTypedRoute> | NextTypedRoute>;
type RouteMatcherWithNextTypedRoutes = Autocomplete<WithPathSegmentWildcard<NextTypedRoute> | NextTypedRoute>;

export type RouteMatcherParam =
| Array<RegExp | RouteMatcherWithNextTypedRoutes>
Expand Down
20 changes: 19 additions & 1 deletion packages/shared/src/pathMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { pathToRegexp } from './pathToRegexp';
import type { Autocomplete } from './types';

/**
* @deprecated Prefer {@link WithPathSegmentWildcard}; `(.*)` also matches sibling routes
* (e.g. `/dashboard(.*)` matches `/dashboardxyz`).
*/
export type WithPathPatternWildcard<T = string> = `${T & string}(.*)`;
export type PathPattern = Autocomplete<WithPathPatternWildcard>;

type StripTrailingSlash<T extends string> = T extends '/'
? T
: T extends `${infer Prefix}/`
? StripTrailingSlash<Prefix>
: T;

/**
* 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 WithPathSegmentWildcard<T = string> = T extends '/'
? '/:path*'
: `${StripTrailingSlash<T & string>}/:path*`;
export type PathPattern = Autocomplete<WithPathSegmentWildcard>;
export type PathMatcherParam = Array<RegExp | PathPattern> | RegExp | PathPattern;

export class MalformedURLError extends Error {
Expand Down
Loading