Skip to content

Commit

Permalink
fix(nextjs): Only ignore paths that have a '.' in the final path segm…
Browse files Browse the repository at this point in the history
…ent (#1695)
  • Loading branch information
BRKalow committed Sep 12, 2023
1 parent 7894ffd commit 64dfbf9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-hounds-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Updates the default middleware config matcher to be more restrictive in how it detects static files. Paths with `.` in them are now allowed, as long as the `.` is not in the final path segment.
13 changes: 12 additions & 1 deletion packages/nextjs/src/server/authMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,20 @@ const validRoutes = [
'/protected',
'/protected/',
'/protected/hello',
'/protected/hello.example/hello',
'/my-protected-page',
'/my/$special/$pages',
];

const invalidRoutes = ['/_next', '/favicon.ico', '/_next/test.json', '/files/api.pdf', '/test/api/test.pdf'];
const invalidRoutes = [
'/_next',
'/favicon.ico',
'/_next/test.json',
'/files/api.pdf',
'/test/api/test.pdf',
'/imgs/img.png',
'/imgs/img-dash.jpg',
];

describe('default config matcher', () => {
it('compiles to regex using path-to-regex', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const INFINITE_REDIRECTION_LOOP_COOKIE = '__clerk_redirection_loop';
* The default ideal matcher that excludes the _next directory (internals) and all static files,
* but it will match the root route (/) and any routes that start with /api or /trpc.
*/
export const DEFAULT_CONFIG_MATCHER = ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'];
export const DEFAULT_CONFIG_MATCHER = ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'];

/**
* Any routes matching this path will be ignored by the middleware.
* This is the inverted version of DEFAULT_CONFIG_MATCHER.
*/
export const DEFAULT_IGNORED_ROUTES = ['/((?!api|trpc))(_next|.+\\..+)(.*)'];
export const DEFAULT_IGNORED_ROUTES = [`/((?!api|trpc))(_next.*|.+\\.[\\w]+$)`];
/**
* Any routes matching this path will be treated as API endpoints by the middleware.
*/
Expand Down

0 comments on commit 64dfbf9

Please sign in to comment.