How to exclude specific routes from Clerk middleware?

Anyone know how I can exclude the middleware from Clerk from running on my routes in folder: /api/cron? Here's the current file:
import { authMiddleware } from "@clerk/nextjs";

// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your Middleware
export default authMiddleware({
debug: false,
});

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
import { authMiddleware } from "@clerk/nextjs";

// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your Middleware
export default authMiddleware({
debug: false,
});

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
I believe we need to update the matcher but not sure how to do it correctly.
1 Reply
sumisura
sumisura7mo ago
Since you are using the older, deprecated authMiddleware, you have two options: 1. Use ignoredRoutes to explicitly exclude that route. 2. Change the regex for matcher to not match /api/cron You can find more info here: https://clerk.com/docs/references/nextjs/auth-middleware I suggest you ignore /api/cron. Just know that this path will not be protected by Clerk. You'll have to handle authentication yourself.
ignoredRoutes? string[]

A list of routes that should be ignored by the middleware. This list typically includes routes for static files or Next.js internals. For improved performance, these routes should be skipped using the default config.matcher instead.
ignoredRoutes? string[]

A list of routes that should be ignored by the middleware. This list typically includes routes for static files or Next.js internals. For improved performance, these routes should be skipped using the default config.matcher instead.
Next.js: authMiddleware()
The authMiddleware() method allows you to protect your Next.js application using middleware.
Want results from more Discord servers?
Add your server