Performance: Locale detection strategies
MEDIUM IMPACT
Locale detection affects the initial page load speed and user interaction responsiveness by determining which language content to serve.
import React from 'react'; export default function Page() { const [locale, setLocale] = React.useState('en'); React.useEffect(() => { const userLocale = navigator.language || 'en'; setLocale(userLocale); }, []); return <div>{`Locale is ${locale}`}</div>; }
export async function getServerSideProps(context) { const locale = context.req.headers['accept-language']?.split(',')[0] || 'en'; return { props: { locale } }; } export default function Page({ locale }) { return <div>{`Locale is ${locale}`}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Server-side locale detection on every request | Minimal | Blocks rendering until locale resolved | Delays paint by 100-200ms | [X] Bad |
| Client-side locale detection after initial render | Minimal | No blocking reflows | Paints immediately, updates locale after | [OK] Good |
| Server-side cookie locale detection | Minimal | Blocks rendering briefly | Adds ~50ms delay to paint | [!] OK |
| Client-side cookie locale detection | Minimal | No blocking reflows | Paints immediately, updates locale after | [OK] Good |
| Static locale detection with i18n routing | Minimal | No runtime reflows | Paints immediately with correct locale | [OK] Good |