0
0
NextJSframework~8 mins

Sub-path routing for locales in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Sub-path routing for locales
MEDIUM IMPACT
This affects the initial page load speed and routing responsiveness by adding locale prefixes to URLs.
Implementing locale-based routing in a Next.js app
NextJS
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
Next.js handles locale routing natively, avoiding manual redirects and reducing server load.
📈 Performance GainEliminates redirect delay, improves LCP by 100-200ms
Implementing locale-based routing in a Next.js app
NextJS
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
Manually redirecting based on Accept-Language header causes extra server-side redirects and delays initial page load.
📉 Performance CostBlocks rendering for 100-200ms due to redirect and extra server processing
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Manual locale redirectsNo extra DOM nodesTriggers 1 reflow due to redirectBlocks paint until redirect completes[X] Bad
Next.js built-in sub-path routingNo extra DOM nodesSingle reflow on initial loadPaints immediately after routing[OK] Good
Rendering Pipeline
Locale sub-path routing affects the routing stage before rendering. The browser requests a URL with a locale prefix, Next.js matches the route and loads the correct locale data before rendering.
Routing
Server-side Rendering
Client-side Hydration
⚠️ BottleneckRouting and server-side rendering can delay initial content if redirects or locale detection are done inefficiently.
Core Web Vital Affected
LCP
This affects the initial page load speed and routing responsiveness by adding locale prefixes to URLs.
Optimization Tips
1Avoid manual redirects for locale detection; use Next.js built-in sub-path routing.
2Configure locales in next.config.js to enable automatic routing prefixes.
3Reducing redirects improves Largest Contentful Paint (LCP) and user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance drawback of manually redirecting users to locale sub-paths based on Accept-Language header?
AIt increases the number of DOM nodes on the page.
BIt causes extra server-side redirects that delay initial page load.
CIt reduces the bundle size significantly.
DIt improves client-side hydration speed.
DevTools: Performance
How to check: Record a page load with locale sub-path routing enabled. Look for any redirect events or delays before the first contentful paint.
What to look for: Check for absence of redirect waterfall and faster Largest Contentful Paint (LCP) timing.