@@ -79,7 +79,7 @@ import accept from '@hapi/accept'
7979import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
8080import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie'
8181import * as Log from '../../build/output/log'
82- import { detectDomainLocales } from '../lib/i18n/detect-domain-locales '
82+ import { detectDomainLocale } from '../lib/i18n/detect-domain-locale '
8383
8484const getCustomRouteMatcher = pathMatch ( true )
8585
@@ -306,67 +306,85 @@ export default class Server {
306306 if ( i18n && ! parsedUrl . pathname ?. startsWith ( '/_next' ) ) {
307307 // get pathname from URL with basePath stripped for locale detection
308308 const { pathname, ...parsed } = parseUrl ( req . url || '/' )
309+ let defaultLocale = i18n . defaultLocale
309310 let detectedLocale = detectLocaleCookie ( req , i18n . locales )
310311
311- const { defaultLocale, locales } = detectDomainLocales (
312- req ,
313- i18n . domains ,
314- i18n . locales ,
315- i18n . defaultLocale
316- )
312+ const detectedDomain = detectDomainLocale ( i18n . domains , req )
313+ if ( detectedDomain ) {
314+ defaultLocale = detectedDomain . defaultLocale
315+ detectedLocale = defaultLocale
316+ }
317317
318318 if ( ! detectedLocale ) {
319319 detectedLocale = accept . language (
320320 req . headers [ 'accept-language' ] ,
321- locales
321+ i18n . locales
322322 )
323323 }
324324
325+ let localeDomainRedirect : string | undefined
326+ const localePathResult = normalizeLocalePath ( pathname ! , i18n . locales )
327+
328+ if ( localePathResult . detectedLocale ) {
329+ detectedLocale = localePathResult . detectedLocale
330+ req . url = formatUrl ( {
331+ ...parsed ,
332+ pathname : localePathResult . pathname ,
333+ } )
334+ parsedUrl . pathname = localePathResult . pathname
335+
336+ // check if the locale prefix matches a domain's defaultLocale
337+ // and we're on a locale specific domain if so redirect to that domain
338+ if ( detectedDomain ) {
339+ const matchedDomain = detectDomainLocale (
340+ i18n . domains ,
341+ undefined ,
342+ detectedLocale
343+ )
344+
345+ if ( matchedDomain ) {
346+ localeDomainRedirect = `http${ matchedDomain . http ? '' : 's' } ://${
347+ matchedDomain ?. domain
348+ } `
349+ }
350+ }
351+ }
352+
325353 const denormalizedPagePath = denormalizePagePath ( pathname || '/' )
326354 const detectedDefaultLocale =
327355 ! detectedLocale ||
328356 detectedLocale . toLowerCase ( ) === defaultLocale . toLowerCase ( )
329357 const shouldStripDefaultLocale =
330358 detectedDefaultLocale &&
331- denormalizedPagePath . toLowerCase ( ) === `/${ defaultLocale . toLowerCase ( ) } `
359+ denormalizedPagePath . toLowerCase ( ) ===
360+ `/${ i18n . defaultLocale . toLowerCase ( ) } `
332361 const shouldAddLocalePrefix =
333362 ! detectedDefaultLocale && denormalizedPagePath === '/'
334363
335- detectedLocale = detectedLocale || defaultLocale
364+ detectedLocale = detectedLocale || i18n . defaultLocale
336365
337366 if (
338367 i18n . localeDetection !== false &&
339- ( shouldAddLocalePrefix || shouldStripDefaultLocale )
368+ ( localeDomainRedirect ||
369+ shouldAddLocalePrefix ||
370+ shouldStripDefaultLocale )
340371 ) {
341372 res . setHeader (
342373 'Location' ,
343374 formatUrl ( {
344375 // make sure to include any query values when redirecting
345376 ...parsed ,
346- pathname : shouldStripDefaultLocale ? '/' : `/${ detectedLocale } ` ,
377+ pathname : localeDomainRedirect
378+ ? localeDomainRedirect
379+ : shouldStripDefaultLocale
380+ ? '/'
381+ : `/${ detectedLocale } ` ,
347382 } )
348383 )
349384 res . statusCode = 307
350385 res . end ( )
351386 return
352387 }
353-
354- const localePathResult = normalizeLocalePath ( pathname ! , locales )
355-
356- if ( localePathResult . detectedLocale ) {
357- detectedLocale = localePathResult . detectedLocale
358- req . url = formatUrl ( {
359- ...parsed ,
360- pathname : localePathResult . pathname ,
361- } )
362- parsedUrl . pathname = localePathResult . pathname
363- }
364-
365- // TODO: render with domain specific locales and defaultLocale also?
366- // Currently locale specific domains will have all locales populated
367- // under router.locales instead of only the domain specific ones
368- parsedUrl . query . __nextLocales = i18n . locales
369- // parsedUrl.query.__nextDefaultLocale = defaultLocale
370388 parsedUrl . query . __nextLocale = detectedLocale || defaultLocale
371389 }
372390
@@ -517,21 +535,15 @@ export default class Server {
517535
518536 if ( i18n ) {
519537 const localePathResult = normalizeLocalePath ( pathname , i18n . locales )
520- const { defaultLocale } = detectDomainLocales (
521- req ,
522- i18n . domains ,
523- i18n . locales ,
524- i18n . defaultLocale
525- )
538+ const { defaultLocale } =
539+ detectDomainLocale ( i18n . domains , req ) || { }
526540 let detectedLocale = defaultLocale
527541
528542 if ( localePathResult . detectedLocale ) {
529543 pathname = localePathResult . pathname
530544 detectedLocale = localePathResult . detectedLocale
531545 }
532- _parsedUrl . query . __nextLocales = i18n . locales
533- _parsedUrl . query . __nextLocale = detectedLocale
534- // _parsedUrl.query.__nextDefaultLocale = defaultLocale
546+ _parsedUrl . query . __nextLocale = detectedLocale !
535547 }
536548 pathname = getRouteFromAssetPath ( pathname , '.json' )
537549
@@ -1078,8 +1090,6 @@ export default class Server {
10781090 amp : query . amp ,
10791091 _nextDataReq : query . _nextDataReq ,
10801092 __nextLocale : query . __nextLocale ,
1081- __nextLocales : query . __nextLocales ,
1082- // __nextDefaultLocale: query.__nextDefaultLocale,
10831093 }
10841094 : query ) ,
10851095 ...( params || { } ) ,
@@ -1151,11 +1161,10 @@ export default class Server {
11511161 delete query . _nextDataReq
11521162
11531163 const locale = query . __nextLocale as string
1154- const locales = query . __nextLocales as string [ ]
1155- // const defaultLocale = query.__nextDefaultLocale as string
11561164 delete query . __nextLocale
1157- delete query . __nextLocales
1158- // delete query.__nextDefaultLocale
1165+
1166+ const { i18n } = this . nextConfig . experimental
1167+ const locales = i18n . locales as string [ ]
11591168
11601169 let previewData : string | false | object | undefined
11611170 let isPreviewMode = false
0 commit comments