Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
7cb68f79
Unverified
Commit
7cb68f79
authored
Oct 27, 2020
by
JJ Kasper
Committed by
GitHub
Oct 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Re-add domain locale redirect handling (#18274)
parent
4026d9be
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
116 additions
and
58 deletions
+116
-58
next-serverless-loader.ts
...ages/next/build/webpack/loaders/next-serverless-loader.ts
+13
-17
detect-domain-locale.ts
packages/next/next-server/lib/i18n/detect-domain-locale.ts
+8
-1
config.ts
packages/next/next-server/server/config.ts
+20
-1
next-server.ts
packages/next/next-server/server/next-server.ts
+18
-18
next.config.js
test/integration/i18n-support/next.config.js
+2
-0
index.test.js
test/integration/i18n-support/test/index.test.js
+55
-21
No files found.
packages/next/build/webpack/loaders/next-serverless-loader.ts
View file @
7cb68f79
...
@@ -262,33 +262,29 @@ const nextServerlessLoader: loader.Loader = function () {
...
@@ -262,33 +262,29 @@ const nextServerlessLoader: loader.Loader = function () {
...parsedUrl,
...parsedUrl,
pathname: localePathResult.pathname,
pathname: localePathResult.pathname,
})
})
req.__nextStrippedLocale = true
parsedUrl.pathname = localePathResult.pathname
parsedUrl.pathname = localePathResult.pathname
}
// If a detected locale is a domain specific locale and we aren't already
// on that domain and path prefix redirect to it to prevent duplicate
// content from multiple domains
if (detectedDomain) {
const localeToCheck = localePathResult.detectedLocale
? detectedLocale
: acceptPreferredLocale
// check if the locale prefix matches a domain's defaultLocale
// and we're on a locale specific domain if so redirect to that domain
// if (detectedDomain) {
// const matchedDomain = detectDomainLocale(
// i18n.domains,
// undefined,
// detectedLocale
// )
// if (matchedDomain) {
// localeDomainRedirect = \`http\${
// matchedDomain.http ? '' : 's'
// }://\${matchedDomain.domain}\`
// }
// }
} else if (detectedDomain) {
const matchedDomain = detectDomainLocale(
const matchedDomain = detectDomainLocale(
i18n.domains,
i18n.domains,
undefined,
undefined,
acceptPreferredLocale
localeToCheck
)
)
if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
localeDomainRedirect = \`http\${matchedDomain.http ? '' : 's'}://\${
localeDomainRedirect = \`http\${matchedDomain.http ? '' : 's'}://\${
matchedDomain.domain
matchedDomain.domain
}/\${
localeToCheck === matchedDomain.defaultLocale ? '' : localeToCheck
}\`
}\`
}
}
}
}
...
...
packages/next/next-server/lib/i18n/detect-domain-locale.ts
View file @
7cb68f79
...
@@ -3,6 +3,7 @@ export function detectDomainLocale(
...
@@ -3,6 +3,7 @@ export function detectDomainLocale(
|
Array
<
{
|
Array
<
{
http
?:
boolean
http
?:
boolean
domain
:
string
domain
:
string
locales
?:
string
[]
defaultLocale
:
string
defaultLocale
:
string
}
>
}
>
|
undefined
,
|
undefined
,
...
@@ -13,17 +14,23 @@ export function detectDomainLocale(
...
@@ -13,17 +14,23 @@ export function detectDomainLocale(
|
{
|
{
http
?:
boolean
http
?:
boolean
domain
:
string
domain
:
string
locales
?:
string
[]
defaultLocale
:
string
defaultLocale
:
string
}
}
|
undefined
|
undefined
if
(
domainItems
)
{
if
(
domainItems
)
{
if
(
detectedLocale
)
{
detectedLocale
=
detectedLocale
.
toLowerCase
()
}
for
(
const
item
of
domainItems
)
{
for
(
const
item
of
domainItems
)
{
// remove port if present
// remove port if present
const
domainHostname
=
item
.
domain
?.
split
(
':'
)[
0
].
toLowerCase
()
const
domainHostname
=
item
.
domain
?.
split
(
':'
)[
0
].
toLowerCase
()
if
(
if
(
hostname
===
domainHostname
||
hostname
===
domainHostname
||
detectedLocale
?.
toLowerCase
()
===
item
.
defaultLocale
.
toLowerCase
()
detectedLocale
===
item
.
defaultLocale
.
toLowerCase
()
||
item
.
locales
?.
some
((
locale
)
=>
locale
.
toLowerCase
()
===
detectedLocale
)
)
{
)
{
domainItem
=
item
domainItem
=
item
break
break
...
...
packages/next/next-server/server/config.ts
View file @
7cb68f79
...
@@ -340,7 +340,26 @@ function assignDefaults(userConfig: { [key: string]: any }) {
...
@@ -340,7 +340,26 @@ function assignDefaults(userConfig: { [key: string]: any }) {
if
(
!
item
.
defaultLocale
)
return
true
if
(
!
item
.
defaultLocale
)
return
true
if
(
!
item
.
domain
||
typeof
item
.
domain
!==
'string'
)
return
true
if
(
!
item
.
domain
||
typeof
item
.
domain
!==
'string'
)
return
true
return
false
let
hasInvalidLocale
=
false
if
(
Array
.
isArray
(
item
.
locales
))
{
for
(
const
locale
of
item
.
locales
)
{
if
(
typeof
locale
!==
'string'
)
hasInvalidLocale
=
true
for
(
const
domainItem
of
i18n
.
domains
)
{
if
(
domainItem
===
item
)
continue
if
(
domainItem
.
locales
&&
domainItem
.
locales
.
includes
(
locale
))
{
console
.
warn
(
`Both
${
item
.
domain
}
and
${
domainItem
.
domain
}
configured the locale (
${
locale
}
) but only one can. Remove it from one i18n.domains config to continue`
)
hasInvalidLocale
=
true
break
}
}
}
}
return
hasInvalidLocale
})
})
if
(
invalidDomainItems
.
length
>
0
)
{
if
(
invalidDomainItems
.
length
>
0
)
{
...
...
packages/next/next-server/server/next-server.ts
View file @
7cb68f79
...
@@ -342,32 +342,32 @@ export default class Server {
...
@@ -342,32 +342,32 @@ export default class Server {
})
})
;(
req
as
any
).
__nextStrippedLocale
=
true
;(
req
as
any
).
__nextStrippedLocale
=
true
parsedUrl
.
pathname
=
localePathResult
.
pathname
parsedUrl
.
pathname
=
localePathResult
.
pathname
}
// If a detected locale is a domain specific locale and we aren't already
// on that domain and path prefix redirect to it to prevent duplicate
// content from multiple domains
if
(
detectedDomain
&&
parsedUrl
.
pathname
===
'/'
)
{
const
localeToCheck
=
acceptPreferredLocale
// const localeToCheck = localePathResult.detectedLocale
// ? detectedLocale
// : acceptPreferredLocale
// check if the locale prefix matches a domain's defaultLocale
// and we're on a locale specific domain if so redirect to that domain
// if (detectedDomain) {
// const matchedDomain = detectDomainLocale(
// i18n.domains,
// undefined,
// detectedLocale
// )
// if (matchedDomain) {
// localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
// matchedDomain?.domain
// }`
// }
// }
}
else
if
(
detectedDomain
)
{
const
matchedDomain
=
detectDomainLocale
(
const
matchedDomain
=
detectDomainLocale
(
i18n
.
domains
,
i18n
.
domains
,
undefined
,
undefined
,
acceptPreferredLocale
localeToCheck
)
)
if
(
matchedDomain
&&
matchedDomain
.
domain
!==
detectedDomain
.
domain
)
{
if
(
matchedDomain
&&
(
matchedDomain
.
domain
!==
detectedDomain
.
domain
||
localeToCheck
!==
matchedDomain
.
defaultLocale
)
)
{
localeDomainRedirect
=
`http
${
matchedDomain
.
http
?
''
:
's'
}
://
${
localeDomainRedirect
=
`http
${
matchedDomain
.
http
?
''
:
's'
}
://
${
matchedDomain
.
domain
matchedDomain
.
domain
}
/
${
localeToCheck
===
matchedDomain
.
defaultLocale
?
''
:
localeToCheck
}
`
}
`
}
}
}
}
...
...
test/integration/i18n-support/next.config.js
View file @
7cb68f79
...
@@ -11,11 +11,13 @@ module.exports = {
...
@@ -11,11 +11,13 @@ module.exports = {
http
:
true
,
http
:
true
,
domain
:
'example.be'
,
domain
:
'example.be'
,
defaultLocale
:
'nl-BE'
,
defaultLocale
:
'nl-BE'
,
locales
:
[
'nl'
,
'nl-NL'
,
'nl-BE'
],
},
},
{
{
http
:
true
,
http
:
true
,
domain
:
'example.fr'
,
domain
:
'example.fr'
,
defaultLocale
:
'fr'
,
defaultLocale
:
'fr'
,
locales
:
[
'fr-BE'
],
},
},
],
],
},
},
...
...
test/integration/i18n-support/test/index.test.js
View file @
7cb68f79
...
@@ -52,11 +52,13 @@ function runTests(isDev) {
...
@@ -52,11 +52,13 @@ function runTests(isDev) {
http
:
true
,
http
:
true
,
domain
:
'example.be'
,
domain
:
'example.be'
,
defaultLocale
:
'nl-BE'
,
defaultLocale
:
'nl-BE'
,
locales
:
[
'nl'
,
'nl-NL'
,
'nl-BE'
],
},
},
{
{
http
:
true
,
http
:
true
,
domain
:
'example.fr'
,
domain
:
'example.fr'
,
defaultLocale
:
'fr'
,
defaultLocale
:
'fr'
,
locales
:
[
'fr-BE'
],
},
},
],
],
})
})
...
@@ -661,27 +663,56 @@ function runTests(isDev) {
...
@@ -661,27 +663,56 @@ function runTests(isDev) {
})
})
it
(
'should handle locales with domain'
,
async
()
=>
{
it
(
'should handle locales with domain'
,
async
()
=>
{
const
checkDomainLocales
=
async
(
domainDefault
=
''
,
domain
=
''
)
=>
{
const
domainItems
=
[
for
(
const
locale
of
locales
)
{
{
// skip other domains' default locale since we redirect these
// used for testing, this should not be needed in most cases
if
([
'fr'
,
'nl-BE'
].
includes
(
locale
)
&&
locale
!==
domainDefault
)
{
// as production domains should always use https
continue
http
:
true
,
}
domain
:
'example.be'
,
defaultLocale
:
'nl-BE'
,
const
res
=
await
fetchViaHTTP
(
locales
:
[
'nl'
,
'nl-NL'
,
'nl-BE'
],
appPort
,
},
`/
${
locale
===
domainDefault
?
''
:
locale
}
`
,
{
undefined
,
http
:
true
,
{
domain
:
'example.fr'
,
headers
:
{
defaultLocale
:
'fr'
,
host
:
domain
,
locales
:
[
'fr-BE'
],
},
},
redirect
:
'manual'
,
]
}
const
domainLocales
=
domainItems
.
reduce
((
prev
,
cur
)
=>
{
)
return
[...
prev
,
...
cur
.
locales
]
},
[])
const
checkDomainLocales
=
async
(
domainDefault
=
''
,
domain
=
''
,
locale
=
''
)
=>
{
const
res
=
await
fetchViaHTTP
(
appPort
,
`/`
,
undefined
,
{
headers
:
{
host
:
domain
,
'accept-language'
:
locale
,
},
redirect
:
'manual'
,
})
const
expectedDomainItem
=
domainItems
.
find
(
(
item
)
=>
item
.
defaultLocale
===
locale
||
item
.
locales
.
includes
(
locale
)
)
const
shouldRedirect
=
expectedDomainItem
.
domain
!==
domain
||
locale
!==
expectedDomainItem
.
defaultLocale
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
status
).
toBe
(
shouldRedirect
?
307
:
200
)
if
(
shouldRedirect
)
{
const
parsedUrl
=
url
.
parse
(
res
.
headers
.
get
(
'location'
),
true
)
expect
(
parsedUrl
.
pathname
).
toBe
(
`/
${
expectedDomainItem
.
defaultLocale
===
locale
?
''
:
locale
}
`
)
expect
(
parsedUrl
.
query
).
toEqual
({})
expect
(
parsedUrl
.
hostname
).
toBe
(
expectedDomainItem
.
domain
)
}
else
{
const
html
=
await
res
.
text
()
const
html
=
await
res
.
text
()
const
$
=
cheerio
.
load
(
html
)
const
$
=
cheerio
.
load
(
html
)
...
@@ -691,8 +722,11 @@ function runTests(isDev) {
...
@@ -691,8 +722,11 @@ function runTests(isDev) {
}
}
}
}
await
checkDomainLocales
(
'nl-BE'
,
'example.be'
)
for
(
const
item
of
domainItems
)
{
await
checkDomainLocales
(
'fr'
,
'example.fr'
)
for
(
const
locale
of
domainLocales
)
{
await
checkDomainLocales
(
item
.
defaultLocale
,
item
.
domain
,
locale
)
}
}
})
})
it
(
'should generate AMP pages with all locales'
,
async
()
=>
{
it
(
'should generate AMP pages with all locales'
,
async
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment