Unverified Commit 7cb68f79 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Re-add domain locale redirect handling (#18274)

parent 4026d9be
...@@ -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
}\` }\`
} }
} }
......
...@@ -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
......
...@@ -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) {
......
...@@ -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
}` }`
} }
} }
......
...@@ -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'],
}, },
], ],
}, },
......
...@@ -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 () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment