Performance: Domain routing for locales
This affects the initial page load speed by determining which locale content is served based on the domain, impacting server response and client rendering.
export default { i18n: { locales: ['en', 'fr'], defaultLocale: 'en', domains: [ { domain: 'example.com', defaultLocale: 'en' }, { domain: 'example.fr', defaultLocale: 'fr' } ] } } // Domain routing for locales
export default { i18n: { locales: ['en', 'fr'], defaultLocale: 'en', localeDetection: true } } // No domain routing, locale chosen by browser or path
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side locale detection with redirects | No extra DOM nodes | Multiple reflows due to content shifts | Higher paint cost from layout shifts | [X] Bad |
| Server-side domain routing for locales | No extra DOM nodes | Single reflow with stable layout | Lower paint cost with stable content | [OK] Good |
i18n key in next.config.js.i18n.domains.i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
domains: [
{ domain: 'example.com', defaultLocale: 'en' },
{ domain: 'example.fr', defaultLocale: 'fr' }
]
}example.fr is mapped to fr locale explicitly.example.fr will serve the French locale fr.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()
}localeDetection: false prevents Next.js from adding locale prefixes in URLs, serving clean domain-based locales.localeDetection: false [OK]Pass the test and this pattern moves unknown