Route & Path Strategies
Relevant source files
The following files were used as context for generating this wiki page:
Nuxt I18n Micro implements a two-tier localization system divided between build-time route generation and runtime path resolution. The architecture uses @i18n-micro/route-strategy to transform standard Nuxt page definitions into a localized route tree during the build process, and @i18n-micro/path-strategy to manage URL resolution, link generation, and redirects at runtime.
Sources: packages/route-strategy/README.md:9-16, packages/path-strategy/README.md:9-15, AGENTS.md:12-13
Architecture Overview
The system separates concerns to ensure high performance and small bundle sizes. Build-time logic handles complex tree mutations, while runtime logic remains tree-shakeable and framework-agnostic.
The diagram shows the transition from build-time static route expansion to runtime dynamic path resolution.
Sources: packages/route-strategy/README.md:20-35, packages/path-strategy/README.md:20-28
Build-Time Route Generation
The RouteGenerator class serves as the primary entry point for build-time operations. It takes a raw pages array and mutates it in-place to include localized variants based on the selected strategy.
Core Generator Responsibilities
- Expansion: Turns a single page definition into multiple routes (e.g.,
/aboutbecomes/en/about,/de/ueber-uns). - Path Mapping: Consolidates
globalLocaleRoutes(custom paths) andfilesLocaleRoutes(in-code definitions). - Restrictions: Applies
routeLocalesto limit which languages a specific page supports. - Consistency: Manages aliases, children, and internal/excluded routes (like
/adminorrobots.txt).
Sources: packages/route-strategy/README.md:20-35, packages/route-strategy/tests/general.test.ts:4-10
Strategy Types and Behavior
| Strategy | URL Behavior | Redirection |
|---|---|---|
no_prefix | No locale in URL (/about). | Managed via cookies or runtime logic. |
prefix | All routes prefixed (/en/about). | No unprefixed default route exists. |
prefix_except_default | Default is clean (/about), others prefixed (/fr/a-propos). | Prefixed default redirects to clean path. |
prefix_and_default | Default is available both ways (/about and /en/about). | Non-default locales are always prefixed. |
Sources: packages/route-strategy/README.md:37-56, packages/route-strategy/tests/strategies.test.ts:8-120
Runtime Path Resolution
@i18n-micro/path-strategy provides the runtime logic for generating links and handling redirects. It is framework-agnostic, interacting with routers through a RouterAdapter interface.
Key Runtime API Methods
localeRoute: Generates a localized route object for a target locale.switchLocaleRoute: Determines the correct path when a user changes languages.getRedirect: Calculated server-side redirect paths (e.g., root to default locale).analyzeRoute: Returns metadata includingbaseName,isIndex, and the locale extracted from the path.
Sources: packages/path-strategy/README.md:11-15, packages/path-strategy/README.md:89-115
The sequence diagram illustrates the resolution of a localized link using strategy-specific logic and a router adapter.
Sources: packages/path-strategy/README.md:65-80, packages/path-strategy/README.md:120-125
Path Normalization and Utilities
The system employs several utilities to ensure deterministic route naming and path consistency across environments.
| Utility | Function |
|---|---|
normalizeRouteKey | Converts Nuxt segments like [id] to :id and [...slug] to :slug(.*)*. |
normalizePath | Strips trailing slashes and resolves double slashes. |
buildRouteName | Constructs localized names (e.g., about + de -> localized-about-de). |
isInternalPath | Identifies paths to exclude from localization, such as _nuxt, sitemap.xml, or api. |
Sources: packages/route-strategy/tests/utils-and-edge.test.ts:18-60, packages/route-strategy/tests/utils-and-edge.test.ts:114-140
Code Example: Custom Path Resolution
const globalLocaleRoutes = {
'/about': {
en: '/about',
de: '/ueber-uns',
ru: '/o-nas',
},
}
// generator.extendPages(pages) uses this map to create localized routes
Sources: packages/route-strategy/README.md:76-83, packages/route-strategy/tests/advanced.test.ts:12-23
Critical Scenarios and Performance
Parent-Child Path Joining
When a parent route has a custom path but the child does not, the generator recursively joins the child's relative segment to the parent's localized path. For example, if /parent becomes /eltern in German, the child /parent/child automatically becomes /eltern/child.
Sources: packages/route-strategy/tests/critical-scenarios.test.ts:18-35
Immutability
The RouteGenerator ensures the original NuxtPage objects are not mutated. It replaces the elements in the pages array with new objects to maintain stability and prevent side effects during the Nuxt build process.
Sources: packages/route-strategy/tests/critical-scenarios.test.ts:223-240
Performance Optimizations
- Tree-shaking: Subpath exports (e.g.,
@i18n-micro/path-strategy/prefix) allow bundling only the required strategy. - Pre-computed Context: Flags like
localeCodesare computed once in the constructor to avoid repeatedObject.keys()allocations. - Memory Management: Uses pure functions for route resolution to minimize object allocation and GC pressure.
Sources: packages/path-strategy/README.md:144-155, packages/path-strategy/README.md:19-24
Route and Path strategies form the backbone of the Nuxt I18n Micro system, enabling complex localization features like custom paths and locale restrictions while maintaining the performance required for large-scale applications.