Domain routing for locales lets your website show different languages or content based on the website address people use. It helps visitors see the right version automatically.
Domain routing for locales in NextJS
module.exports = { i18n: { locales: ['en', 'fr', 'de'], defaultLocale: 'en', domains: [ { domain: 'example.com', defaultLocale: 'en' }, { domain: 'example.fr', defaultLocale: 'fr' }, { domain: 'example.de', defaultLocale: 'de' } ] } };
This code goes inside next.config.js to tell Next.js about your locales and domains.
The domains array links each domain to a default language.
module.exports = { i18n: { locales: ['en', 'es'], defaultLocale: 'en', domains: [ { domain: 'mysite.com', defaultLocale: 'en' }, { domain: 'mysite.es', defaultLocale: 'es' } ] } };
module.exports = { i18n: { locales: ['en', 'ja'], defaultLocale: 'en', domains: [ { domain: 'example.com', defaultLocale: 'en' }, { domain: 'example.jp', defaultLocale: 'ja' } ] } };
This example configures two domains for English and French. The page shows a greeting based on the current locale detected from the domain.
/* next.config.js */ module.exports = { i18n: { locales: ['en', 'fr'], defaultLocale: 'en', domains: [ { domain: 'myapp.com', defaultLocale: 'en' }, { domain: 'myapp.fr', defaultLocale: 'fr' } ] } }; /* pages/index.js */ import { useRouter } from 'next/router'; export default function Home() { const { locale } = useRouter(); return ( <main> <h1>{locale === 'fr' ? 'Bonjour!' : 'Hello!'}</h1> <p>{locale === 'fr' ? 'Bienvenue sur notre site.' : 'Welcome to our site.'}</p> </main> ); }
Make sure your DNS and hosting are set up to point each domain to your Next.js app.
Next.js automatically detects the locale from the domain and serves the right content.
You can add more domains and locales by extending the domains array.
Domain routing lets you serve different languages on different website addresses.
Configure domains and locales in next.config.js under the i18n key.
Your app can show content based on the detected locale from the domain.
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
