rsnext/packages/next/next-server/lib/i18n/detect-domain-locale.ts
JJ Kasper 8be3562c48
Ensure correct default locale is detected for domain locale (#18046)
This makes sure that we detect the correct default locale for domain specific locales since a domain can have a different default locale residing at the root and we need to check this on the client for prerendered/auto-static pages. This also makes sure we disable the built-in redirect handling when on Vercel since it's handled already. 

Tests for this are tricky since we need to load the browser with a custom domain which requires editing the host file. Existing tests should ensure this doesn't break non-domain specific locale behavior though. This was also tested manually while testing https://github.com/vercel/vercel/pull/5298



x-ref: https://github.com/vercel/next.js/pull/17370
2020-10-20 10:23:11 +00:00

35 lines
722 B
TypeScript

export function detectDomainLocale(
domainItems:
| Array<{
http?: boolean
domain: string
defaultLocale: string
}>
| undefined,
hostname?: string,
detectedLocale?: string
) {
let domainItem:
| {
http?: boolean
domain: string
defaultLocale: string
}
| undefined
if (domainItems) {
for (const item of domainItems) {
// remove port if present
const domainHostname = item.domain?.split(':')[0].toLowerCase()
if (
hostname === domainHostname ||
detectedLocale?.toLowerCase() === item.defaultLocale.toLowerCase()
) {
domainItem = item
break
}
}
}
return domainItem
}