Skip to content

Commit

Permalink
Make sure to only redirect domain on locale root
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 26, 2020
1 parent 431ad4b commit b6fe1c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
9 changes: 4 additions & 5 deletions packages/next/build/webpack/loaders/next-serverless-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ const nextServerlessLoader: loader.Loader = function () {
// content from multiple domains
if (detectedDomain) {
const localeToCheck = localePathResult.detectedLocale
? detectedLocale
: acceptPreferredLocale
? detectedLocale
: acceptPreferredLocale
const matchedDomain = detectDomainLocale(
i18n.domains,
Expand All @@ -283,9 +283,8 @@ const nextServerlessLoader: loader.Loader = function () {
if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
localeDomainRedirect = \`http\${matchedDomain.http ? '' : 's'}://\${
matchedDomain.domain
}/\${localeToCheck === matchedDomain.defaultLocale
? ''
: localeToCheck
}/\${
localeToCheck === matchedDomain.defaultLocale ? '' : localeToCheck
}\`
}
}
Expand Down
21 changes: 20 additions & 1 deletion packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,26 @@ function assignDefaults(userConfig: { [key: string]: any }) {
if (!item.defaultLocale) 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) {
Expand Down
15 changes: 10 additions & 5 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,23 @@ export default class Server {
// 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
if (detectedDomain && parsedUrl.pathname === '/') {
const localeToCheck = acceptPreferredLocale
// const localeToCheck = localePathResult.detectedLocale
// ? detectedLocale
// : acceptPreferredLocale

const matchedDomain = detectDomainLocale(
i18n.domains,
undefined,
localeToCheck
)

if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
if (
matchedDomain &&
(matchedDomain.domain !== detectedDomain.domain ||
localeToCheck !== matchedDomain.defaultLocale)
) {
localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
matchedDomain.domain
}/${
Expand Down
22 changes: 10 additions & 12 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,21 +688,19 @@ function runTests(isDev) {
domain = '',
locale = ''
) => {
const res = await fetchViaHTTP(
appPort,
`/${locale === domainDefault ? '' : locale}`,
undefined,
{
headers: {
host: domain,
},
redirect: 'manual',
}
)
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
const shouldRedirect =
expectedDomainItem.domain !== domain ||
locale !== expectedDomainItem.defaultLocale

expect(res.status).toBe(shouldRedirect ? 307 : 200)

Expand Down

0 comments on commit b6fe1c0

Please sign in to comment.