diff --git a/packages/next/server/router.ts b/packages/next/server/router.ts index 26a5168776994..32226b9a3139d 100644 --- a/packages/next/server/router.ts +++ b/packages/next/server/router.ts @@ -15,6 +15,7 @@ import { matchHas } from '../shared/lib/router/utils/prepare-destination' import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix' import { getRequestMeta } from './request-meta' import { addPathPrefix } from '../shared/lib/router/utils/add-path-prefix' +import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info' type RouteResult = { finished: boolean @@ -300,6 +301,21 @@ export default class Router { // if it is set let currentPathname = parsedUrlUpdated.pathname as string const originalPathname = currentPathname + const pathnameInfo = getNextPathnameInfo(originalPathname, { + nextConfig: this.nextConfig, + parseData: false, + }) + + // don't match API routes when they are locale prefixed + // e.g. /api/hello shouldn't match /en/api/hello as a page + // rewrites/redirects can match though + if ( + !customRouteTypes.has(testRoute.type) && + pathnameInfo.locale && + pathnameInfo.pathname.match(/^\/api(?:\/|$)/) + ) { + continue + } const currentPathnameNoBasePath = removePathPrefix( currentPathname, @@ -314,26 +330,10 @@ export default class Router { currentPathname = currentPathnameNoBasePath } - const localePathResult = normalizeLocalePath( - currentPathnameNoBasePath, - this.locales - ) - - // don't match API routes when they are locale prefixed - // e.g. /api/hello shouldn't match /en/api/hello as a page - // rewrites/redirects can match though - if ( - !customRouteTypes.has(testRoute.type) && - localePathResult.detectedLocale && - localePathResult.pathname.match(/^\/api(?:\/|$)/) - ) { - continue - } - if ( customRouteTypes.has(testRoute.type) && !testRoute.internal && - !localePathResult.detectedLocale && + !pathnameInfo.locale && parsedUrl.query.__nextLocale ) { currentPathname = addPathPrefix(