@@ -24,7 +24,6 @@ interface BuildResponseOptions {
24
24
request : Request
25
25
result : FetchEventResult
26
26
nextConfig ?: RequestData [ 'nextConfig' ]
27
- requestLocale ?: string
28
27
}
29
28
30
29
export const buildResponse = async ( {
@@ -33,7 +32,6 @@ export const buildResponse = async ({
33
32
request,
34
33
result,
35
34
nextConfig,
36
- requestLocale,
37
35
} : BuildResponseOptions ) : Promise < Response | void > => {
38
36
logger
39
37
. withFields ( { is_nextresponse_next : result . response . headers . has ( 'x-middleware-next' ) } )
@@ -197,11 +195,10 @@ export const buildResponse = async ({
197
195
return addMiddlewareHeaders ( context . rewrite ( target ) , res )
198
196
}
199
197
200
- // If we are redirecting a request that had a locale in the URL, we need to add it back in
201
- if ( redirect && requestLocale ) {
202
- redirect = normalizeLocalizedTarget ( { target : redirect , request, nextConfig, requestLocale } )
198
+ if ( redirect ) {
199
+ redirect = normalizeLocalizedTarget ( { target : redirect , request, nextConfig } )
203
200
if ( redirect === request . url ) {
204
- logger . withFields ( { rewrite_url : rewrite } ) . debug ( 'Rewrite url is same as original url' )
201
+ logger . withFields ( { redirect_url : redirect } ) . debug ( 'Redirect url is same as original url' )
205
202
return
206
203
}
207
204
res . headers . set ( 'location' , redirect )
@@ -234,25 +231,25 @@ function normalizeLocalizedTarget({
234
231
target,
235
232
request,
236
233
nextConfig,
237
- requestLocale,
238
234
} : {
239
235
target : string
240
236
request : Request
241
237
nextConfig ?: RequestData [ 'nextConfig' ]
242
- requestLocale ?: string
243
- } ) {
238
+ } ) : string {
244
239
const targetUrl = new URL ( target , request . url )
245
240
246
241
const normalizedTarget = normalizeLocalePath ( targetUrl . pathname , nextConfig ?. i18n ?. locales )
247
242
248
- const locale = normalizedTarget . detectedLocale ?? requestLocale
249
243
if (
250
- locale &&
244
+ normalizedTarget . detectedLocale &&
251
245
! normalizedTarget . pathname . startsWith ( `/api/` ) &&
252
246
! normalizedTarget . pathname . startsWith ( `/_next/static/` )
253
247
) {
254
248
targetUrl . pathname =
255
- addBasePath ( `/${ locale } ${ normalizedTarget . pathname } ` , nextConfig ?. basePath ) || `/`
249
+ addBasePath (
250
+ `/${ normalizedTarget . detectedLocale } ${ normalizedTarget . pathname } ` ,
251
+ nextConfig ?. basePath ,
252
+ ) || `/`
256
253
} else {
257
254
targetUrl . pathname = addBasePath ( normalizedTarget . pathname , nextConfig ?. basePath ) || `/`
258
255
}
0 commit comments