Skip to content

Commit 7f3bbbf

Browse files
committed
Deprecate createRouteMatcher in nextjs
1 parent 7733d51 commit 7f3bbbf

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@clerk/nextjs': patch
3+
---
4+
5+
Deprecate `createRouteMatcher()` in favor of resource-based auth checks.
6+
7+
Middleware-based auth checks rely on path matching, which can diverge from how Next.js routes requests and leave protected resources reachable.
8+
9+
For a migration guide, see:
10+
https://clerk.com/docs/guides/development/upgrading/upgrade-guides/migrating-from-create-route-matcher
11+
12+
Instead of protecting routes from middleware, move auth checks into each protected page, layout, API route, or Server Function, for example:
13+
14+
```ts
15+
import { auth } from '@clerk/nextjs/server'
16+
17+
export default async function Page() {
18+
await auth.protect()
19+
20+
return <h1>Dashboard</h1>
21+
}
22+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
import { createRouteMatcher } from '../routeMatcher';
4+
5+
const { mockDeprecated } = vi.hoisted(() => ({
6+
mockDeprecated: vi.fn(),
7+
}));
8+
9+
vi.mock('@clerk/shared/deprecated', () => ({
10+
deprecated: mockDeprecated,
11+
}));
12+
13+
describe('createRouteMatcher', () => {
14+
beforeEach(() => {
15+
mockDeprecated.mockClear();
16+
});
17+
18+
it('should emit a deprecation warning when called', () => {
19+
createRouteMatcher(['/foo(.*)']);
20+
21+
expect(mockDeprecated).toHaveBeenCalledWith(
22+
'createRouteMatcher',
23+
'Use resource-based auth checks instead. Move auth checks into each page, layout, API route, or Server Function that accesses protected data. Middleware-based auth checks rely on path matching, which can diverge from how Next.js routes requests and leave protected resources reachable. For a migration guide, see: https://clerk.com/docs/guides/development/upgrading/upgrade-guides/migrating-from-create-route-matcher',
24+
);
25+
});
26+
});

‎packages/nextjs/src/server/routeMatcher.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { deprecated } from '@clerk/shared/deprecated';
12
import { createPathMatcher, type WithPathPatternWildcard } from '@clerk/shared/pathMatcher';
23
import type { Autocomplete } from '@clerk/shared/types';
34
import type Link from 'next/link';
@@ -19,8 +20,20 @@ export type RouteMatcherParam =
1920
* You can use glob patterns to match multiple routes or a function to match against the request object.
2021
* Path patterns and regular expressions are supported, for example: `['/foo', '/bar(.*)'] or `[/^\/foo\/.*$/]`
2122
* For more information, see: https://clerk.com/docs
23+
*
24+
* @deprecated This function will be removed in the next major version. Use resource-based auth checks instead.
25+
* Move auth checks into each page, layout, API route, or Server Function that accesses protected data.
26+
* Middleware-based auth checks rely on path matching, which can diverge from how Next.js routes requests and
27+
* leave protected resources reachable.
28+
*
29+
* For a migration guide, see:
30+
* https://clerk.com/docs/guides/development/upgrading/upgrade-guides/migrating-from-create-route-matcher
2231
*/
2332
export const createRouteMatcher = (routes: RouteMatcherParam) => {
33+
deprecated(
34+
'createRouteMatcher',
35+
'Use resource-based auth checks instead. Move auth checks into each page, layout, API route, or Server Function that accesses protected data. Middleware-based auth checks rely on path matching, which can diverge from how Next.js routes requests and leave protected resources reachable. For a migration guide, see: https://clerk.com/docs/guides/development/upgrading/upgrade-guides/migrating-from-create-route-matcher',
36+
);
2437
if (typeof routes === 'function') {
2538
return (req: NextRequest) => routes(req);
2639
}

0 commit comments

Comments
 (0)