diff --git a/packages/next/src/shared/lib/router/utils/middleware-route-matcher.ts b/packages/next/src/shared/lib/router/utils/middleware-route-matcher.ts index f97f7746fc6f..1fb398acb654 100644 --- a/packages/next/src/shared/lib/router/utils/middleware-route-matcher.ts +++ b/packages/next/src/shared/lib/router/utils/middleware-route-matcher.ts @@ -14,13 +14,17 @@ export interface MiddlewareRouteMatch { export function getMiddlewareRouteMatcher( matchers: ProxyMatcher[] ): MiddlewareRouteMatch { + const compiled = matchers.map((matcher) => ({ + ...matcher, + re: new RegExp(matcher.regexp), + })) return ( pathname: string | null | undefined, req: BaseNextRequest, query: Params ) => { - for (const matcher of matchers) { - const routeMatch = new RegExp(matcher.regexp).exec(pathname!) + for (const matcher of compiled) { + const routeMatch = matcher.re.exec(pathname!) if (!routeMatch) { continue }