Skip to content

Commit

Permalink
feat: run Clerk middleware only needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ixartz committed May 7, 2024
1 parent 757d2ab commit 5aeee06
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/middleware.ts
@@ -1,4 +1,5 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import type { NextFetchEvent, NextRequest } from 'next/server';
import createMiddleware from 'next-intl/middleware';

import { AppConfig } from './utils/AppConfig';
Expand All @@ -14,11 +15,24 @@ const isProtectedRoute = createRouteMatcher([
'/:locale/dashboard(.*)',
]);

export default clerkMiddleware((auth, req) => {
if (isProtectedRoute(req)) auth().protect();
export default function middleware(
request: NextRequest,
event: NextFetchEvent,
) {
if (
request.nextUrl.pathname.includes('/sign-in') ||
request.nextUrl.pathname.includes('/sign-up') ||
isProtectedRoute(request)
) {
return clerkMiddleware((auth, req) => {
if (isProtectedRoute(req)) auth().protect();

return intlMiddleware(req);
});
return intlMiddleware(req);
})(request, event);
}

return intlMiddleware(request);
}

export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
Expand Down

0 comments on commit 5aeee06

Please sign in to comment.