Skip to content

Commit 7a80195

Browse files
authored
Update routes generation for build-complete (#85017)
## Summary - Adds comprehensive route generation and normalization for build-complete adapter hooks - Enhances adapter output types with additional metadata fields (`sourcePage`, `wasmAssets`, `pprChain`, `parentFallbackMode`) - Improves middleware matcher handling and revalidation bypass logic - Adds proper tracing for shared node dependencies across pages and app router modules - Includes `buildId` in adapter context for build identification ## Changes ### Route Generation - Generates complete route manifests with processed regex patterns for headers, redirects, rewrites, and dynamic routes - Converts routes using `@vercel/routing-utils` for consistent formatting - Handles RSC routes (`.rsc` suffix) for app router pages - Supports i18n locale-specific routes and data routes (`/_next/data/[buildId]/...`) - Properly handles static pages with mixed app/pages router setups ### Adapter Output Enhancements - Added `sourcePage` field to all route outputs for tracking original source files - Added `wasmAssets` support for bundled WebAssembly files - Added `pprChain` and `parentFallbackMode` to prerender outputs for PPR support - Enhanced middleware matchers to include processed source and regex patterns - Improved traced assets handling with separate tracking for pages vs app router modules ### Middleware & Revalidation - Standardized middleware matcher format with `source`, `sourceRegex`, `has`, and `missing` fields - Automatically adds `x-prerender-revalidate` header bypass to middleware matchers - Fixed middleware pathname to always be `/_middleware` for consistency ### Asset Tracing - Traces shared node dependencies for bootstrapping (node-environment, require-hook, etc.) - Separate tracing for pages router and app router module contexts - Improved trace ignores for unnecessary files (dev builds, webpack internals, etc.) - Better handling of instrumentation hook assets ### Bug Fixes - Fixed RSC fallback handling for mixed app/pages router builds - Improved 404/500 static page detection with locale support - Better basePath normalization with trailing slash handling - Fixed data route generation for getServerSideProps pages with i18n
1 parent 6c10513 commit 7a80195

File tree

23 files changed

+1425
-163
lines changed

23 files changed

+1425
-163
lines changed

docs/01-app/03-api-reference/05-config/01-next-config-js/adapterPath.mdx

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,17 @@ Called after the build process completes with detailed information about routes
128128
**Parameters:**
129129

130130
- `routes`: Object containing route manifests for headers, redirects, rewrites, and dynamic routes
131+
- `routes.headers`: Array of header route objects with `source`, `sourceRegex`, `headers`, `has`, `missing`, and optional `priority` fields
132+
- `routes.redirects`: Array of redirect route objects with `source`, `sourceRegex`, `destination`, `statusCode`, `has`, `missing`, and optional `priority` fields
133+
- `routes.rewrites`: Object with `beforeFiles`, `afterFiles`, and `fallback` arrays, each containing rewrite route objects with `source`, `sourceRegex`, `destination`, `has`, and `missing` fields
134+
- `routes.dynamicRoutes`: Array of dynamic route objects with `source`, `sourceRegex`, `destination`, `has`, and `missing` fields
131135
- `outputs`: Detailed information about all build outputs organized by type
132136
- `projectDir`: Absolute path to the Next.js project directory
133137
- `repoRoot`: Absolute path to the detected repository root
134138
- `distDir`: Absolute path to the build output directory
135139
- `config`: The final Next.js configuration (with `modifyConfig` applied)
136140
- `nextVersion`: Version of Next.js being used
141+
- `buildId`: Unique identifier for the current build
137142

138143
## Output Types
139144

@@ -149,11 +154,14 @@ React pages from the `pages/` directory:
149154
id: string // Route identifier
150155
filePath: string // Path to the built file
151156
pathname: string // URL pathname
157+
sourcePage: string // Original source file path in pages/ directory
152158
runtime: 'nodejs' | 'edge'
153-
assets: Record<string, string> // Traced dependencies
159+
assets: Record<string, string> // Traced dependencies (key: relative path from repo root, value: absolute path)
160+
wasmAssets?: Record<string, string> // Bundled wasm files (key: name, value: absolute path)
154161
config: {
155162
maxDuration?: number
156163
preferredRegion?: string | string[]
164+
env?: Record<string, string> // Environment variables (edge runtime only)
157165
}
158166
}
159167
```
@@ -168,11 +176,14 @@ API routes from `pages/api/`:
168176
id: string
169177
filePath: string
170178
pathname: string
179+
sourcePage: string // Original relative source file path
171180
runtime: 'nodejs' | 'edge'
172181
assets: Record<string, string>
182+
wasmAssets?: Record<string, string>
173183
config: {
174184
maxDuration?: number
175185
preferredRegion?: string | string[]
186+
env?: Record<string, string>
176187
}
177188
}
178189
```
@@ -186,12 +197,15 @@ React pages from the `app/` directory with `page.{js,ts,jsx,tsx}`:
186197
type: 'APP_PAGE'
187198
id: string
188199
filePath: string
189-
pathname: string
200+
pathname: string // Includes .rsc suffix for RSC routes
201+
sourcePage: string // Original relative source file path
190202
runtime: 'nodejs' | 'edge'
191203
assets: Record<string, string>
204+
wasmAssets?: Record<string, string>
192205
config: {
193206
maxDuration?: number
194207
preferredRegion?: string | string[]
208+
env?: Record<string, string>
195209
}
196210
}
197211
```
@@ -203,7 +217,18 @@ API and metadata routes from `app/` with `route.{js,ts,jsx,tsx}`:
203217
```typescript
204218
{
205219
type: 'APP_ROUTE'
206-
// ... same structure as APP_PAGE
220+
id: string
221+
filePath: string
222+
pathname: string
223+
sourcePage: string
224+
runtime: 'nodejs' | 'edge'
225+
assets: Record<string, string>
226+
wasmAssets?: Record<string, string>
227+
config: {
228+
maxDuration?: number
229+
preferredRegion?: string | string[]
230+
env?: Record<string, string>
231+
}
207232
}
208233
```
209234

@@ -217,7 +242,11 @@ ISR-enabled routes and static prerenders:
217242
id: string
218243
pathname: string
219244
parentOutputId: string // ID of the source page/route
220-
groupId: number // Revalidation group identifier
245+
groupId: number // Revalidation group identifier (prerenders with same groupId revalidate together)
246+
pprChain?: {
247+
headers: Record<string, string> // PPR chain headers (e.g., 'x-nextjs-resume': '1')
248+
}
249+
parentFallbackMode?: 'blocking' | false | null // Fallback mode from getStaticPaths
221250
fallback?: {
222251
filePath: string
223252
initialStatus?: number
@@ -258,17 +287,72 @@ Middleware function (if present):
258287
type: 'MIDDLEWARE'
259288
id: string
260289
filePath: string
261-
pathname: string
290+
pathname: string // Always '/_middleware'
291+
sourcePage: string // Always 'middleware'
262292
runtime: 'nodejs' | 'edge'
263293
assets: Record<string, string>
294+
wasmAssets?: Record<string, string>
264295
config: {
265296
maxDuration?: number
266297
preferredRegion?: string | string[]
267-
matchers?: ProxyMatcher[]
298+
env?: Record<string, string>
299+
matchers?: Array<{
300+
source: string
301+
sourceRegex: string
302+
has: RouteHas[] | undefined
303+
missing: RouteHas[] | undefined
304+
}>
268305
}
269306
}
270307
```
271308

309+
## Routes Information
310+
311+
The `routes` object in `onBuildComplete` provides complete routing information with processed patterns ready for deployment:
312+
313+
### Headers
314+
315+
Each header route includes:
316+
317+
- `source`: Original route pattern (e.g., `/about`)
318+
- `sourceRegex`: Compiled regex for matching requests
319+
- `headers`: Key-value pairs of headers to apply
320+
- `has`: Optional conditions that must be met
321+
- `missing`: Optional conditions that must not be met
322+
- `priority`: Optional flag for internal routes
323+
324+
### Redirects
325+
326+
Each redirect route includes:
327+
328+
- `source`: Original route pattern
329+
- `sourceRegex`: Compiled regex for matching
330+
- `destination`: Target URL (can include captured groups)
331+
- `statusCode`: HTTP status code (301, 302, 307, 308)
332+
- `has`: Optional positive conditions
333+
- `missing`: Optional negative conditions
334+
- `priority`: Optional flag for internal routes
335+
336+
### Rewrites
337+
338+
Rewrites are categorized into three phases:
339+
340+
- `beforeFiles`: Checked before filesystem (including pages and public files)
341+
- `afterFiles`: Checked after pages/public files but before dynamic routes
342+
- `fallback`: Checked after all other routes
343+
344+
Each rewrite includes `source`, `sourceRegex`, `destination`, `has`, and `missing`.
345+
346+
### Dynamic Routes
347+
348+
Generated from dynamic route segments (e.g., `[slug]`, `[...path]`). Each includes:
349+
350+
- `source`: Route pattern
351+
- `sourceRegex`: Compiled regex with named capture groups
352+
- `destination`: Internal destination with parameter substitution
353+
- `has`: Optional positive conditions
354+
- `missing`: Optional negative conditions
355+
272356
## Use Cases
273357

274358
Common use cases for adapters include:
@@ -278,3 +362,4 @@ Common use cases for adapters include:
278362
- **Monitoring Integration**: Collect build metrics and route information
279363
- **Custom Bundling**: Package outputs in platform-specific formats
280364
- **Build Validation**: Ensure outputs meet specific requirements
365+
- **Route Generation**: Use processed route information to generate platform-specific routing configs

packages/next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
"@types/ws": "8.2.0",
219219
"@vercel/ncc": "0.34.0",
220220
"@vercel/nft": "0.27.1",
221+
"@vercel/routing-utils": "5.2.0",
221222
"@vercel/turbopack-ecmascript-runtime": "*",
222223
"acorn": "8.14.0",
223224
"anser": "1.4.9",

0 commit comments

Comments
 (0)