next.js
7429f7ce - refactor: extract route discovery into unified `discoverRoutes()` API (#88971)

Commit
24 days ago
refactor: extract route discovery into unified `discoverRoutes()` API (#88971) ## Summary Route collection, mapping, and processing logic was duplicated across six call sites (`build/index.ts`, `build/analyze/index.ts`, `cli/next-typegen.ts`, `server/lib/router-utils/setup-dev-bundler.ts`, `server/mcp/tools/get-routes.ts`, and `build/entries.ts`). Each independently orchestrated the same sequence: collect files → create page mappings → process routes → extract slots. This PR extracts that logic into two focused modules: - **`build/route-discovery.ts`** — Exports a high-level `discoverRoutes()` function that handles the full collect → map → process pipeline in one call. Also re-exports the lower-level helpers (`createPagesMapping`, `collectAppFiles`, etc.) for call sites that need them. - **`build/file-classifier.ts`** — Shared types (`RouteInfo`, `SlotInfo`) and slot extraction utilities (`extractSlotFromPath`, `addSlotIfNew`, `combineSlots`) used by both the build and dev server. ## Bug fix Fixes a bug in the dev bundler (`setup-dev-bundler.ts`) where `hasRootAppNotFound` was set to `true` unconditionally for every app directory file, rather than only when an actual `not-found` file was encountered. This caused the server to incorrectly report the existence of a root not-found boundary for any project with an app directory. ## Performance - **Eliminates a duplicate directory traversal** — The old build code called `collectAppFiles()` twice: once during the main flow and again during route type generation to get `defaultPaths`. Now all three file categories (pages, layouts, defaults) are collected in a single `recursiveReadDir` call. - **Parallelizes app file mapping** — App pages, layouts, and default files are now mapped concurrently via `Promise.all` instead of sequentially. - **MCP tool parallelism** — The `get-routes` MCP tool now discovers app and pages routes in parallel instead of sequentially. ## Changes - `build/entries.ts` — Removed ~470 lines of collection/mapping/processing functions (moved to `route-discovery.ts` and `file-classifier.ts`). Retains entrypoint creation logic. - `build/index.ts` — Replaced inline orchestration with a single `discoverRoutes()` call. - `build/analyze/index.ts` — Replaced inline orchestration with `discoverRoutes()`. - `cli/next-typegen.ts` — Replaced ~60 lines of inline orchestration with `discoverRoutes()`. - `server/lib/router-utils/setup-dev-bundler.ts` — Replaced inline slot detection with `addSlotIfNew()`. Fixed `hasRootAppNotFound` bug. - `server/mcp/tools/get-routes.ts` — Replaced ~80 lines of manual collection with two parallel `discoverRoutes()` calls. - `server/lib/find-page-file.ts` — Deduplicated regex construction in `createValidFileMatcher` using helper factories. - `server/lib/router-utils/route-types-utils.ts` — Updated to use shared `RouteInfo`/`SlotInfo` types from `file-classifier.ts`.
Author
Parents
Loading