What if your website could magically show the right language just by the domain users visit?
Why Domain routing for locales in NextJS? - Purpose & Use Cases
Imagine you have a website that serves users in different countries, each with its own language and domain like example.com for English and example.fr for French.
You try to manually redirect users based on their language preference by checking the URL and rewriting links everywhere.
Manually managing redirects and links for each locale is confusing and error-prone.
You might forget to update some links or mix languages, causing a bad user experience.
It also makes your code messy and hard to maintain as your site grows.
Domain routing for locales lets Next.js automatically serve the right language version based on the domain.
This means you configure your domains once, and Next.js handles routing users to the correct locale seamlessly.
if (url.includes('/fr')) { redirectTo('example.fr'); } else { redirectTo('example.com'); }
module.exports = { i18n: { locales: ['en', 'fr'], defaultLocale: 'en', domains: [{ domain: 'example.com', defaultLocale: 'en' }, { domain: 'example.fr', defaultLocale: 'fr' }] } }You can easily support multiple languages with their own domains, giving users a smooth, localized experience without extra code.
A global e-commerce site uses domain routing to show prices and content in the local language and currency automatically when users visit their country-specific domain.
Manual locale redirects are hard to manage and error-prone.
Domain routing automates locale detection based on domain names.
This leads to cleaner code and better user experience across languages.
Practice
Solution
Step 1: Understand domain routing concept
Domain routing allows serving different languages or locales on separate domains or subdomains.Step 2: Identify the main use case in Next.js
Next.js uses domain routing to link domains to specific locales for internationalization.Final Answer:
To serve different languages on different domains -> Option DQuick Check:
Domain routing = serve languages on domains [OK]
- Confusing domain routing with caching
- Thinking it only affects client-side routing
- Assuming it creates multiple apps
Solution
Step 1: Recall Next.js i18n config structure
The internationalization config uses thei18nkey innext.config.js.Step 2: Identify the property for domain routing
The correct property to define domain-locale mapping isi18n.domains.Final Answer:
i18n.domains -> Option BQuick Check:
Domain routing config = i18n.domains [OK]
- Using next.config.domains instead of i18n.domains
- Confusing with routing.locales
- Trying to set domains outside i18n
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
domains: [
{ domain: 'example.com', defaultLocale: 'en' },
{ domain: 'example.fr', defaultLocale: 'fr' }
]
}Solution
Step 1: Check domain-locale mapping in config
The domainexample.fris mapped tofrlocale explicitly.Step 2: Determine locale served for example.fr
Requests toexample.frwill serve the French localefr.Final Answer:
fr -> Option AQuick Check:
example.fr domain = fr locale [OK]
- Assuming defaultLocale 'en' applies to all domains
- Thinking browser locale overrides domain
- Believing error occurs without explicit locale
export function middleware(request) {
const hostname = request.headers.get('host')
if (hostname === 'example.fr') {
return NextResponse.rewrite(new URL('/fr', request.url))
}
return NextResponse.next()
}Solution
Step 1: Understand middleware role in locale detection
Middleware should set locale or rewrite URL to include locale prefix properly.Step 2: Identify issue with rewriting only
Rewriting to '/fr' alone may not set locale context; locale cookie or header is needed for Next.js to recognize locale.Final Answer:
Rewriting to '/fr' instead of setting locale cookie -> Option AQuick Check:
Middleware must set locale, not just rewrite URL [OK]
- Assuming host header is wrong
- Forgetting to handle all domains
- Missing export default in middleware
Solution
Step 1: Define locales and defaultLocale correctly
Locales should be ['en', 'fr'] with defaultLocale 'en' to serve English by default.Step 2: Configure i18n.domains for domain-locale mapping
Map 'example.com' to 'en' and 'example.fr' to 'fr' in i18n.domains array.Step 3: Disable localeDetection to avoid prefix in URLs
SettinglocaleDetection: falseprevents Next.js from adding locale prefixes in URLs, serving clean domain-based locales.Final Answer:
Set i18n.locales to ['en', 'fr'], defaultLocale to 'en', and i18n.domains with domains for example.com (en) and example.fr (fr), and use localeDetection: false -> Option CQuick Check:
Domain routing + defaultLocale + no prefix = Set i18n.locales to ['en', 'fr'], defaultLocale to 'en', and i18n.domains with domains for example.com (en) and example.fr (fr), and uselocaleDetection: false[OK]
- Swapping locales for domains
- Forgetting to disable localeDetection
- Relying only on middleware without domains
Ready to prove it?
Pass the test and this pattern moves unknown
