From b6fe1c0b7918582f8b2e7d88a3139f925f094478 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 26 Oct 2020 18:50:53 -0500 Subject: [PATCH] Make sure to only redirect domain on locale root --- .../webpack/loaders/next-serverless-loader.ts | 9 ++++---- packages/next/next-server/server/config.ts | 21 +++++++++++++++++- .../next/next-server/server/next-server.ts | 15 ++++++++----- .../i18n-support/test/index.test.js | 22 +++++++++---------- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index 33a903cf1b5a5..7ca949a47f82d 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -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, @@ -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 }\` } } diff --git a/packages/next/next-server/server/config.ts b/packages/next/next-server/server/config.ts index 1940286b801c1..cc0b10570ba12 100644 --- a/packages/next/next-server/server/config.ts +++ b/packages/next/next-server/server/config.ts @@ -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) { diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index b91b23e8fd106..b6dac836c2685 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -347,10 +347,11 @@ 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, @@ -358,7 +359,11 @@ export default class Server { localeToCheck ) - if (matchedDomain && matchedDomain.domain !== detectedDomain.domain) { + if ( + matchedDomain && + (matchedDomain.domain !== detectedDomain.domain || + localeToCheck !== matchedDomain.defaultLocale) + ) { localeDomainRedirect = `http${matchedDomain.http ? '' : 's'}://${ matchedDomain.domain }/${ diff --git a/test/integration/i18n-support/test/index.test.js b/test/integration/i18n-support/test/index.test.js index 9b9ef69f6d979..83d22086629be 100644 --- a/test/integration/i18n-support/test/index.test.js +++ b/test/integration/i18n-support/test/index.test.js @@ -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)