Select protected middleware routes
A middleware needs to apply authentication protection only to selected dashboard or API paths.
Clerk · Function
createRouteMatcher() builds a predicate from an array of Next.js-style route patterns. Calling the predicate with a middleware request returns true when the request matches a supplied route; Clerk now marks the helper deprecated.
Pass an array of Next.js-style route patterns to createRouteMatcher. It returns a predicate that accepts the middleware request and returns true when the request path matches one of those patterns, allowing middleware logic such as auth.protect() to run conditionally.
createRouteMatcher(routes: string[]): (req: Request) => boolean| Name | Type | Description |
|---|---|---|
routes | string[] | An array of route patterns using Next.js Middleware matcher-style syntax. |
A function that accepts the Next.js Middleware req object and returns true when its route matches one of the supplied patterns.
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
const isDashboardRoute = createRouteMatcher(['/dashboard/:path*'])
export default clerkMiddleware(async (auth, req) => {
if (isDashboardRoute(req)) {
await auth.protect()
}
})Creates a predicate for dashboard paths and protects matching requests in Clerk middleware.
A middleware needs to apply authentication protection only to selected dashboard or API paths.
Several related paths need to share one route predicate.
It accepts an array of route patterns and returns a function. Calling that function with the middleware request returns true when the request matches any supplied pattern.
Follow-up: How should a dashboard subtree be represented? Use /dashboard/:path*, which maps directly to router path-segment matching.
The patterns use the same format as paths supplied to the Next.js Middleware matcher. This lets one predicate cover several route patterns.
Follow-up: What should be used for non-authentication path decisions? Use the framework's native matcher and req.nextUrl.pathname.
It was commonly used inside clerkMiddleware() to conditionally call authentication protection for matching requests. Current Clerk guidance recommends protecting each server-side resource instead of relying on middleware-based auth checks.
Follow-up: What migration direction should a new implementation follow? Move checks into the individual page, Route Handler, Server Function, or Server Action that accesses protected data.
clerkMiddlewareauth.protectconfig.matcherreq.nextUrl.pathname