Route generation process
Routes are generated in a specific order to ensure correct precedence (index.ts:261-936):
Route types
The adapter processes several types of routes from Next.js configuration.Redirects
Redirects are extracted and categorized by priority (routing.ts:176-211):
continue: true to allow subsequent routes to process, while normal redirects terminate routing.
Redirects are identified by status codes: 301, 302, 303, 307, or 308.
Rewrites
Rewrites allow URLs to be rewritten to different destinations without changing the browser URL. They’re organized in three phases (routing.ts:143-171):
beforeFiles rewrites
beforeFiles rewrites
Run before checking the filesystem. Marked with
override: true to take precedence over static files.afterFiles rewrites
afterFiles rewrites
Run after checking the filesystem but before dynamic routes. Uses
check: true to verify the destination exists.fallback rewrites
fallback rewrites
Run as a last resort if no other routes match.
Headers
Custom headers fromnext.config.js are extracted and applied with continue: true (routing.ts:216-244):
important: true to ensure they’re not overridden.
Dynamic routes
Dynamic routes use regex patterns to match parameterized URLs (index.ts:231-259):
Routing handles
Vercel uses special “handle” routes to control routing flow:| Handle | Purpose | Location |
|---|---|---|
filesystem | Check if static file exists | After beforeFiles rewrites |
resource | Check for API routes and functions | After afterFiles rewrites |
miss | Continue if no match found | Before error handling |
hit | Match found, apply final headers | After dynamic routes |
rewrite | Apply rewrite transformations | After fallback rewrites |
error | Handle errors (404/500) | End of routing |
Rewrite headers
For App Router with PPR, rewrites get special headers to preserve routing information (routing.ts:10-124):
x-nextjs-rewritten-path: The original pathname before rewritex-nextjs-rewritten-query: Query parameters from the rewrite
Internal Next.js query params (starting with
nxtP or nxtI) are filtered out to avoid conflicts.i18n routing
For applications with internationalization, the adapter generates routes for (index.ts:282-420):
Locale detection
Automatic locale detection based on headers and cookies:Domain-based locales
Redirect users to locale-specific domains:Default locale handling
Routes to strip and add default locale prefixes:Middleware routing
Middleware routes are generated from middleware matchers (outputs.ts:814-827):
Next.js data routes
For Pages Router with middleware,_next/data URLs require special handling (routing.ts:246-318):
Normalization
Strip the_next/data prefix and .json extension:
Denormalization
After processing, convert back to data URLs (routing.ts:337-392):
RSC and prefetch routing
App Router requires special routes for React Server Components (index.ts:500-598):
RSC requests
Routes that detect and rewrite RSC requests:Segment prefetching
For PPR, segment prefetch requests are routed to special files:Conditional routing
Routes can conditionally match based on headers, cookies, or query parameters:Has conditions
Missing conditions
Error handling routes
The adapter generates routes for 404 and 500 errors (index.ts:433-489, 846-935):
The adapter automatically detects which error pages exist and generates appropriate routes.
Route debugging
To debug routing issues, examine the generatedconfig.json: