@@ -24,7 +24,6 @@ interface BuildResponseOptions {
2424 request : Request
2525 result : FetchEventResult
2626 nextConfig ?: RequestData [ 'nextConfig' ]
27- requestLocale ?: string
2827}
2928
3029export const buildResponse = async ( {
@@ -33,7 +32,6 @@ export const buildResponse = async ({
3332 request,
3433 result,
3534 nextConfig,
36- requestLocale,
3735} : BuildResponseOptions ) : Promise < Response | void > => {
3836 logger
3937 . withFields ( { is_nextresponse_next : result . response . headers . has ( 'x-middleware-next' ) } )
@@ -197,11 +195,10 @@ export const buildResponse = async ({
197195 return addMiddlewareHeaders ( context . rewrite ( target ) , res )
198196 }
199197
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 } )
203200 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' )
205202 return
206203 }
207204 res . headers . set ( 'location' , redirect )
@@ -234,25 +231,25 @@ function normalizeLocalizedTarget({
234231 target,
235232 request,
236233 nextConfig,
237- requestLocale,
238234} : {
239235 target : string
240236 request : Request
241237 nextConfig ?: RequestData [ 'nextConfig' ]
242- requestLocale ?: string
243- } ) {
238+ } ) : string {
244239 const targetUrl = new URL ( target , request . url )
245240
246241 const normalizedTarget = normalizeLocalePath ( targetUrl . pathname , nextConfig ?. i18n ?. locales )
247242
248- const locale = normalizedTarget . detectedLocale ?? requestLocale
249243 if (
250- locale &&
244+ normalizedTarget . detectedLocale &&
251245 ! normalizedTarget . pathname . startsWith ( `/api/` ) &&
252246 ! normalizedTarget . pathname . startsWith ( `/_next/static/` )
253247 ) {
254248 targetUrl . pathname =
255- addBasePath ( `/${ locale } ${ normalizedTarget . pathname } ` , nextConfig ?. basePath ) || `/`
249+ addBasePath (
250+ `/${ normalizedTarget . detectedLocale } ${ normalizedTarget . pathname } ` ,
251+ nextConfig ?. basePath ,
252+ ) || `/`
256253 } else {
257254 targetUrl . pathname = addBasePath ( normalizedTarget . pathname , nextConfig ?. basePath ) || `/`
258255 }
0 commit comments