Performance: Sub-path routing for locales
This affects the initial page load speed and routing responsiveness by adding locale prefixes to URLs.
module.exports = { i18n: { locales: ['en', 'fr', 'es'], defaultLocale: 'en', localeDetection: true } }; // Next.js built-in sub-path routing configured in next.config.js // Pages are automatically served under /en, /fr, /es prefixes
export async function getServerSideProps(context) { const locale = context.req.headers['accept-language']?.split(',')[0] || 'en'; if (!context.params.locale) { return { redirect: { destination: `/${locale}${context.resolvedUrl}`, permanent: false } }; } return { props: {} }; } // No built-in Next.js locale routing used
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual locale redirects | No extra DOM nodes | Triggers 1 reflow due to redirect | Blocks paint until redirect completes | [X] Bad |
| Next.js built-in sub-path routing | No extra DOM nodes | Single reflow on initial load | Paints immediately after routing | [OK] Good |
/en or /fr to show different language versions of the same site./en or /fr. correctly describes this purpose. Other options describe unrelated or incorrect behaviors.next.config.js for sub-path routing?i18n object with locales array and defaultLocale string./fr?
import { useRouter } from 'next/router';
export default function Home() {
const { locale } = useRouter();
return <div>Current language: {locale}</div>;
}useRouter hook provides the current locale from the URL sub-path like /fr./fr, locale will be 'fr', so the rendered text is "Current language: fr".next.config.js snippet for locale sub-path routing:
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
localeDetection: true,
},
};localeDetection inside i18n to enable or disable automatic locale detection.localeDetection: true enables automatic detection, which is allowed./fr/about to show French content. Which steps are needed to correctly configure this?
A) Set i18n.locales to ['en', 'fr'] and defaultLocale to 'en' in next.config.js.
B) Use useRouter() in pages to get locale and show language-specific content.
C) Create folders or files under app or pages to handle routes for each locale.
D) Disable locale detection to force manual locale switching.i18n.locales and defaultLocale in next.config.js enables sub-path routing for English and French.useRouter() to get locale helps render language-specific content dynamically.app or pages supports localized routes like /fr/about.Pass the test and this pattern moves unknown