@@ -5,7 +5,7 @@ import nextConfig from './next.config.json' with { type: 'json' }
5
5
6
6
import { InternalHeaders } from './lib/headers.ts'
7
7
import { logger , LogLevel } from './lib/logging.ts'
8
- import { buildNextRequest , RequestData } from './lib/next-request.ts'
8
+ import { buildNextRequest , localizeRequest , RequestData } from './lib/next-request.ts'
9
9
import { buildResponse , FetchEventResult } from './lib/response.ts'
10
10
import {
11
11
getMiddlewareRouteMatcher ,
@@ -31,25 +31,29 @@ export async function handleMiddleware(
31
31
context : Context ,
32
32
nextHandler : NextHandler ,
33
33
) {
34
- const nextRequest = buildNextRequest ( request , context , nextConfig )
35
34
const url = new URL ( request . url )
35
+
36
36
const reqLogger = logger
37
37
. withLogLevel (
38
38
request . headers . has ( InternalHeaders . NFDebugLogging ) ? LogLevel . Debug : LogLevel . Log ,
39
39
)
40
40
. withFields ( { url_path : url . pathname } )
41
41
. withRequestID ( request . headers . get ( InternalHeaders . NFRequestID ) )
42
42
43
+ const { localizedUrl } = localizeRequest ( url , nextConfig )
43
44
// While we have already checked the path when mapping to the edge function,
44
45
// Next.js supports extra rules that we need to check here too, because we
45
46
// might be running an edge function for a path we should not. If we find
46
47
// that's the case, short-circuit the execution.
47
- if ( ! matchesMiddleware ( url . pathname , request , searchParamsToUrlQuery ( url . searchParams ) ) ) {
48
+ if (
49
+ ! matchesMiddleware ( localizedUrl . pathname , request , searchParamsToUrlQuery ( url . searchParams ) )
50
+ ) {
48
51
reqLogger . debug ( 'Aborting middleware due to runtime rules' )
49
52
50
53
return
51
54
}
52
55
56
+ const nextRequest = buildNextRequest ( request , context , nextConfig )
53
57
try {
54
58
const result = await nextHandler ( { request : nextRequest } )
55
59
const response = await buildResponse ( {
0 commit comments