Skip to content

Commit 529e2e1

Browse files
authored
feat(nextjs): Introduce stable createRouteMatcher (#2572)
* feat(nextjs): Introduce stable createRouteMatcher * Create hungry-lies-burn.md * Create hungry-lies-burn.md
1 parent f52035c commit 529e2e1

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

.changeset/hungry-lies-burn.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@clerk/nextjs": patch
3+
---
4+
5+
Introduce `createRouteMatcher` which is designed to generate and return a function that evaluates whether a given Request object matches a set of predefined routes. It provides flexibility in defining these routes through various patterns, including glob patterns, regular expressions, and custom functions. This composable helper can be used in combination with the `clerkMiddleware` helper to easily protect specific routes, eg:
6+
```ts
7+
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
8+
9+
const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']);
10+
11+
export default clerkMiddleware((auth, request) => {
12+
if (isProtectedRoute(request)) {
13+
auth().protect();
14+
}
15+
});
16+
```

packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ exports[`/server public exports should not include a breaking change 1`] = `
88
"clerkClient",
99
"clerkMiddleware",
1010
"createClerkClient",
11+
"createRouteMatcher",
1112
"currentUser",
12-
"experimental_createRouteMatcher",
1313
"getAuth",
1414
"redirectToSignIn",
1515
"redirectToSignUp",
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Generic exports
33
*/
4-
import { createRouteMatcher } from './routeMatcher';
4+
export { createRouteMatcher } from './routeMatcher';
55

66
export { verifyToken, createClerkClient } from '@clerk/backend';
77
export type { WebhookEvent, WebhookEventType } from '@clerk/backend';
@@ -16,13 +16,3 @@ export { auth } from '../app-router/server/auth';
1616
export { currentUser } from '../app-router/server/currentUser';
1717
export { authMiddleware } from './authMiddleware';
1818
export { clerkMiddleware } from './clerkMiddleware';
19-
20-
/**
21-
* Returns a function that accepts a `Request` object and returns whether the request matches the list of
22-
* predefined routes that can be passed in as the first argument.
23-
*
24-
* You can use glob patterns to match multiple routes or a function to match against the request object.
25-
* Path patterns and regular expressions are supported, for example: `['/foo', '/bar(.*)'] or `[/^\/foo\/.*$/]`
26-
* For more information, see: https://clerk.com/docs
27-
*/
28-
export const experimental_createRouteMatcher = createRouteMatcher;

packages/nextjs/src/server/routeMatcher.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import { paths } from '../utils';
77
type WithPathPatternWildcard<T> = `${T & string}(.*)`;
88
type NextTypedRoute<T = Parameters<typeof Link>['0']['href']> = T extends string ? T : never;
99

10-
// For extra safety, we won't recommend using a `/(.*)` route matcher.
11-
type ExcludeRootPath<T> = T extends '/' ? never : T;
12-
13-
type RouteMatcherWithNextTypedRoutes = Autocomplete<
14-
WithPathPatternWildcard<ExcludeRootPath<NextTypedRoute>> | NextTypedRoute
15-
>;
10+
type RouteMatcherWithNextTypedRoutes = Autocomplete<WithPathPatternWildcard<NextTypedRoute> | NextTypedRoute>;
1611

1712
export type RouteMatcherParam =
1813
| Array<RegExp | RouteMatcherWithNextTypedRoutes>
@@ -21,9 +16,12 @@ export type RouteMatcherParam =
2116
| ((req: NextRequest) => boolean);
2217

2318
/**
24-
* Create a function that matches a request against the specified routes.
25-
* Precomputes the glob matchers for the public routes, so we don't have to
26-
* recompile the regular expressions on every request.
19+
* Returns a function that accepts a `Request` object and returns whether the request matches the list of
20+
* predefined routes that can be passed in as the first argument.
21+
*
22+
* You can use glob patterns to match multiple routes or a function to match against the request object.
23+
* Path patterns and regular expressions are supported, for example: `['/foo', '/bar(.*)'] or `[/^\/foo\/.*$/]`
24+
* For more information, see: https://clerk.com/docs
2725
*/
2826
export const createRouteMatcher = (routes: RouteMatcherParam) => {
2927
if (typeof routes === 'function') {

0 commit comments

Comments
 (0)