0
0
NextJSframework~10 mins

Domain routing for locales in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Domain routing for locales
User enters URL
Next.js checks domain
Match domain to locale config?
NoUse default locale
Yes
Set locale based on domain
Render page with locale
User sees localized content
Next.js checks the domain the user visits, matches it to a locale, then renders the page in that locale.
Execution Sample
NextJS
export const nextConfig = {
  i18n: {
    locales: ['en', 'fr'],
    defaultLocale: 'en',
    domains: [
      { domain: 'example.com', defaultLocale: 'en' },
      { domain: 'exemple.fr', defaultLocale: 'fr' }
    ]
  }
}
This config sets English for example.com and French for exemple.fr domains.
Execution Table
StepUser URLDomain MatchedLocale SetPage Rendered
1https://example.com/homeexample.comenHome page in English
2https://exemple.fr/homeexemple.frfrHome page in French
3https://unknown.com/homeNo matchen (default)Home page in English
4https://example.com/aboutexample.comenAbout page in English
5https://exemple.fr/aboutexemple.frfrAbout page in French
💡 Execution stops after rendering the localized page based on domain or default locale.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
userURLN/Ahttps://example.com/homehttps://exemple.fr/homehttps://unknown.com/homehttps://example.com/abouthttps://exemple.fr/about
matchedDomainN/Aexample.comexemple.frNo matchexample.comexemple.fr
localeen (default)enfren (default)enfr
pageRenderedN/AHome page in EnglishHome page in FrenchHome page in EnglishAbout page in EnglishAbout page in French
Key Moments - 2 Insights
Why does the locale default to 'en' when the domain is unknown?
Because the execution_table row 3 shows 'No match' for domain, so Next.js uses the defaultLocale 'en' as configured.
How does Next.js know which locale to use for a domain?
Next.js matches the domain from the user URL to the domains array in the config, as shown in execution_table rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what locale is set when the user visits 'https://exemple.fr/home'?
Aen
BdefaultLocale
Cfr
DNo locale
💡 Hint
Check row 2 under 'Locale Set' in the execution_table.
At which step does the domain not match any configured domain?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look for 'No match' in the 'Domain Matched' column in execution_table.
If we add a new domain 'example.de' with defaultLocale 'de', what would happen when visiting 'https://example.de/home'?
ALocale would be 'de' and page rendered in German
BLocale would be 'en' as default
CNo locale set, error occurs
DLocale would be 'fr'
💡 Hint
Refer to how domains map to locales in the execution_table and variable_tracker.
Concept Snapshot
Next.js domain routing for locales:
- Configure i18n.domains with domain and defaultLocale
- User visits URL
- Next.js matches domain to locale
- If no match, uses defaultLocale
- Page renders in matched locale
- Enables localized content by domain
Full Transcript
Domain routing for locales in Next.js means the app looks at the domain name the user visits. It checks if the domain matches any configured domain in the i18n.domains array. If it matches, Next.js sets the locale to the domain's defaultLocale. If no domain matches, it uses the defaultLocale set globally. Then the page renders with content in that locale. For example, example.com shows English content, while exemple.fr shows French. This lets websites serve different languages on different domains automatically.