File tree Expand file tree Collapse file tree 6 files changed +40
-15
lines changed Expand file tree Collapse file tree 6 files changed +40
-15
lines changed Original file line number Diff line number Diff line change @@ -1012,9 +1012,12 @@ export default async function getBaseWebpackConfig(
1012
1012
'process.env.__NEXT_IMAGE_OPTS' : JSON . stringify ( config . images ) ,
1013
1013
'process.env.__NEXT_ROUTER_BASEPATH' : JSON . stringify ( config . basePath ) ,
1014
1014
'process.env.__NEXT_HAS_REWRITES' : JSON . stringify ( hasRewrites ) ,
1015
- 'process.env.__NEXT_i18n_SUPPORT ' : JSON . stringify (
1015
+ 'process.env.__NEXT_I18N_SUPPORT ' : JSON . stringify (
1016
1016
! ! config . experimental . i18n
1017
1017
) ,
1018
+ 'process.env.__NEXT_I18N_DOMAINS' : JSON . stringify (
1019
+ config . experimental . i18n . domains
1020
+ ) ,
1018
1021
'process.env.__NEXT_ANALYTICS_ID' : JSON . stringify (
1019
1022
config . experimental . analyticsId
1020
1023
) ,
Original file line number Diff line number Diff line change @@ -233,9 +233,13 @@ const nextServerlessLoader: loader.Loader = function () {
233
233
i18n.locales
234
234
)
235
235
236
+ const { host } = req.headers || {}
237
+ // remove port from host and remove port if present
238
+ const hostname = host && host.split(':')[0].toLowerCase()
239
+
236
240
const detectedDomain = detectDomainLocale(
237
241
i18n.domains,
238
- req ,
242
+ hostname ,
239
243
)
240
244
if (detectedDomain) {
241
245
defaultLocale = detectedDomain.defaultLocale
@@ -291,6 +295,7 @@ const nextServerlessLoader: loader.Loader = function () {
291
295
if (
292
296
!fromExport &&
293
297
!nextStartMode &&
298
+ !req.headers["${ vercelHeader } "] &&
294
299
i18n.localeDetection !== false &&
295
300
(
296
301
localeDomainRedirect ||
Original file line number Diff line number Diff line change @@ -83,11 +83,15 @@ if (hasBasePath(asPath)) {
83
83
asPath = delBasePath ( asPath )
84
84
}
85
85
86
- if ( process . env . __NEXT_i18n_SUPPORT ) {
86
+ if ( process . env . __NEXT_I18N_SUPPORT ) {
87
87
const {
88
88
normalizeLocalePath,
89
89
} = require ( '../next-server/lib/i18n/normalize-locale-path' )
90
90
91
+ const {
92
+ detectDomainLocale,
93
+ } = require ( '../next-server/lib/i18n/detect-domain-locale' )
94
+
91
95
if ( locales ) {
92
96
const localePathResult = normalizeLocalePath ( asPath , locales )
93
97
@@ -99,6 +103,18 @@ if (process.env.__NEXT_i18n_SUPPORT) {
99
103
// locales
100
104
defaultLocale = locale
101
105
}
106
+
107
+ // attempt detecting default locale based on hostname
108
+ const detectedDomain = detectDomainLocale (
109
+ process . env . __NEXT_I18N_DOMAINS ,
110
+ window . location . hostname
111
+ )
112
+
113
+ // TODO: investigate if defaultLocale needs to be populated after
114
+ // hydration to prevent mismatched renders
115
+ if ( detectedDomain ) {
116
+ defaultLocale = detectedDomain . defaultLocale
117
+ }
102
118
}
103
119
}
104
120
Original file line number Diff line number Diff line change 1
- import { IncomingMessage } from 'http'
2
-
3
1
export function detectDomainLocale (
4
2
domainItems :
5
3
| Array < {
@@ -8,7 +6,7 @@ export function detectDomainLocale(
8
6
defaultLocale : string
9
7
} >
10
8
| undefined ,
11
- req ?: IncomingMessage ,
9
+ hostname ?: string ,
12
10
detectedLocale ?: string
13
11
) {
14
12
let domainItem :
@@ -20,10 +18,6 @@ export function detectDomainLocale(
20
18
| undefined
21
19
22
20
if ( domainItems ) {
23
- const { host } = req ?. headers || { }
24
- // remove port from host and remove port if present
25
- const hostname = host ?. split ( ':' ) [ 0 ] . toLowerCase ( )
26
-
27
21
for ( const item of domainItems ) {
28
22
// remove port if present
29
23
const domainHostname = item . domain ?. split ( ':' ) [ 0 ] . toLowerCase ( )
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ export function addLocale(
61
61
locale ?: string ,
62
62
defaultLocale ?: string
63
63
) {
64
- if ( process . env . __NEXT_i18n_SUPPORT ) {
64
+ if ( process . env . __NEXT_I18N_SUPPORT ) {
65
65
return locale && locale !== defaultLocale && ! path . startsWith ( '/' + locale )
66
66
? addPathPrefix ( path , '/' + locale )
67
67
: path
@@ -70,7 +70,7 @@ export function addLocale(
70
70
}
71
71
72
72
export function delLocale ( path : string , locale ?: string ) {
73
- if ( process . env . __NEXT_i18n_SUPPORT ) {
73
+ if ( process . env . __NEXT_I18N_SUPPORT ) {
74
74
return locale && path . startsWith ( '/' + locale )
75
75
? path . substr ( locale . length + 1 ) || '/'
76
76
: path
@@ -453,7 +453,7 @@ export default class Router implements BaseRouter {
453
453
454
454
this . isFallback = isFallback
455
455
456
- if ( process . env . __NEXT_i18n_SUPPORT ) {
456
+ if ( process . env . __NEXT_I18N_SUPPORT ) {
457
457
this . locale = locale
458
458
this . locales = locales
459
459
this . defaultLocale = defaultLocale
Original file line number Diff line number Diff line change @@ -318,7 +318,11 @@ export default class Server {
318
318
i18n . locales
319
319
)
320
320
321
- const detectedDomain = detectDomainLocale ( i18n . domains , req )
321
+ const { host } = req ?. headers || { }
322
+ // remove port from host and remove port if present
323
+ const hostname = host ?. split ( ':' ) [ 0 ] . toLowerCase ( )
324
+
325
+ const detectedDomain = detectDomainLocale ( i18n . domains , hostname )
322
326
if ( detectedDomain ) {
323
327
defaultLocale = detectedDomain . defaultLocale
324
328
detectedLocale = defaultLocale
@@ -564,9 +568,12 @@ export default class Server {
564
568
const { i18n } = this . nextConfig . experimental
565
569
566
570
if ( i18n ) {
571
+ const { host } = req ?. headers || { }
572
+ // remove port from host and remove port if present
573
+ const hostname = host ?. split ( ':' ) [ 0 ] . toLowerCase ( )
567
574
const localePathResult = normalizeLocalePath ( pathname , i18n . locales )
568
575
const { defaultLocale } =
569
- detectDomainLocale ( i18n . domains , req ) || { }
576
+ detectDomainLocale ( i18n . domains , hostname ) || { }
570
577
let detectedLocale = defaultLocale
571
578
572
579
if ( localePathResult . detectedLocale ) {
You can’t perform that action at this time.
0 commit comments