Skip to content

Commit b6fe1c0

Browse files
committed
Make sure to only redirect domain on locale root
1 parent 431ad4b commit b6fe1c0

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

packages/next/build/webpack/loaders/next-serverless-loader.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ const nextServerlessLoader: loader.Loader = function () {
271271
// content from multiple domains
272272
if (detectedDomain) {
273273
const localeToCheck = localePathResult.detectedLocale
274-
? detectedLocale
275-
: acceptPreferredLocale
274+
? detectedLocale
275+
: acceptPreferredLocale
276276
277277
const matchedDomain = detectDomainLocale(
278278
i18n.domains,
@@ -283,9 +283,8 @@ const nextServerlessLoader: loader.Loader = function () {
283283
if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
284284
localeDomainRedirect = \`http\${matchedDomain.http ? '' : 's'}://\${
285285
matchedDomain.domain
286-
}/\${localeToCheck === matchedDomain.defaultLocale
287-
? ''
288-
: localeToCheck
286+
}/\${
287+
localeToCheck === matchedDomain.defaultLocale ? '' : localeToCheck
289288
}\`
290289
}
291290
}

packages/next/next-server/server/config.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,26 @@ function assignDefaults(userConfig: { [key: string]: any }) {
340340
if (!item.defaultLocale) return true
341341
if (!item.domain || typeof item.domain !== 'string') return true
342342

343-
return false
343+
let hasInvalidLocale = false
344+
345+
if (Array.isArray(item.locales)) {
346+
for (const locale of item.locales) {
347+
if (typeof locale !== 'string') hasInvalidLocale = true
348+
349+
for (const domainItem of i18n.domains) {
350+
if (domainItem === item) continue
351+
if (domainItem.locales && domainItem.locales.includes(locale)) {
352+
console.warn(
353+
`Both ${item.domain} and ${domainItem.domain} configured the locale (${locale}) but only one can. Remove it from one i18n.domains config to continue`
354+
)
355+
hasInvalidLocale = true
356+
break
357+
}
358+
}
359+
}
360+
}
361+
362+
return hasInvalidLocale
344363
})
345364

346365
if (invalidDomainItems.length > 0) {

packages/next/next-server/server/next-server.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,23 @@ export default class Server {
347347
// If a detected locale is a domain specific locale and we aren't already
348348
// on that domain and path prefix redirect to it to prevent duplicate
349349
// content from multiple domains
350-
if (detectedDomain) {
351-
const localeToCheck = localePathResult.detectedLocale
352-
? detectedLocale
353-
: acceptPreferredLocale
350+
if (detectedDomain && parsedUrl.pathname === '/') {
351+
const localeToCheck = acceptPreferredLocale
352+
// const localeToCheck = localePathResult.detectedLocale
353+
// ? detectedLocale
354+
// : acceptPreferredLocale
354355

355356
const matchedDomain = detectDomainLocale(
356357
i18n.domains,
357358
undefined,
358359
localeToCheck
359360
)
360361

361-
if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) {
362+
if (
363+
matchedDomain &&
364+
(matchedDomain.domain !== detectedDomain.domain ||
365+
localeToCheck !== matchedDomain.defaultLocale)
366+
) {
362367
localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${
363368
matchedDomain.domain
364369
}/${

test/integration/i18n-support/test/index.test.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -688,21 +688,19 @@ function runTests(isDev) {
688688
domain = '',
689689
locale = ''
690690
) => {
691-
const res = await fetchViaHTTP(
692-
appPort,
693-
`/${locale === domainDefault ? '' : locale}`,
694-
undefined,
695-
{
696-
headers: {
697-
host: domain,
698-
},
699-
redirect: 'manual',
700-
}
701-
)
691+
const res = await fetchViaHTTP(appPort, `/`, undefined, {
692+
headers: {
693+
host: domain,
694+
'accept-language': locale,
695+
},
696+
redirect: 'manual',
697+
})
702698
const expectedDomainItem = domainItems.find(
703699
(item) => item.defaultLocale === locale || item.locales.includes(locale)
704700
)
705-
const shouldRedirect = expectedDomainItem.domain !== domain
701+
const shouldRedirect =
702+
expectedDomainItem.domain !== domain ||
703+
locale !== expectedDomainItem.defaultLocale
706704

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

0 commit comments

Comments
 (0)