Replace glob scan with fs-based traversal in dev routes API - #65
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewer's GuideReplaces glob-based page discovery in the dev routes API with an internal fs-based recursive scanner while preserving existing route parsing, filtering, and labeling behavior. Sequence diagram for GET handler using fs-based page discoverysequenceDiagram
participant Client
participant DevRoutesAPI as DevRoutesAPI_GET
participant FS as FileSystem
Client->>DevRoutesAPI: HTTP GET /api/dev/routes
DevRoutesAPI->>DevRoutesAPI: determine cwd and appDir
DevRoutesAPI->>FS: findPageFiles(appDir)
activate FS
FS->>FS: read entries in currentDir
FS->>FS: recursively traverse subdirectories
FS->>FS: filter api, .*, _* directories
FS->>FS: collect page.{tsx,js,jsx} relative paths
FS-->>DevRoutesAPI: list of page files
deactivate FS
DevRoutesAPI->>DevRoutesAPI: map files to route paths
DevRoutesAPI->>DevRoutesAPI: split dirPath by path.sep
DevRoutesAPI->>DevRoutesAPI: clean segments and build labels
DevRoutesAPI->>DevRoutesAPI: remove duplicates and sort routes
DevRoutesAPI-->>Client: JSON response with discovered routes
Flow diagram for findPageFiles recursive page scannerflowchart TD
A["start findPageFiles(rootDir,currentDir)"] --> B["read directory entries withFileTypes"]
B --> C["map each entry"]
C --> D{"entry name startsWith . or _?"}
D -- yes --> E["return empty list for this entry"]
D -- no --> F["compute fullPath = join(currentDir,name)<br/>relativePath = relative(rootDir,fullPath)"]
F --> G{"entry isDirectory?"}
G -- yes --> H{"entry name === api?"}
H -- yes --> E
H -- no --> I["recurse findPageFiles(rootDir,fullPath)<br/>collect returned files"]
G -- no --> J{"entry isFile?"}
J -- no --> E
J -- yes --> K["parsed = path.parse(name)<br/>check parsed.name === page<br/>and ext in PAGE_EXTENSIONS"]
K --> L{"is page file?"}
L -- yes --> M["return list containing relativePath"]
L -- no --> E
I --> N["flatten lists from all entries"]
M --> N
E --> N
N --> O["return flattened file list"]
O --> P["end findPageFiles"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
|
||
| export const dynamic = 'force-dynamic' // Ensure this route is dynamic | ||
|
|
||
| const PAGE_EXTENSIONS = new Set(['.tsx', '.js', '.jsx']) |
There was a problem hiding this comment.
missing .ts extension - Next.js app router supports page.ts files
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| async function findPageFiles(rootDir: string, currentDir = rootDir): Promise<string[]> { | ||
| const entries = await readdir(currentDir, { withFileTypes: true }) | ||
| const files = await Promise.all(entries.map(async entry => { | ||
| if (entry.name.startsWith('.') || entry.name.startsWith('_')) return [] |
There was a problem hiding this comment.
underscore filtering differs from glob behavior - glob ignored **/_*/** (folders starting with underscore), but this skips any file/folder starting with underscore at any level, potentially missing valid pages in non-underscore folders
Additional Comments (1)
|
Motivation
Module not found: Can't resolve 'glob'for the dev routes API, so the route discovery should avoid requiring the externalglobpackage.Description
globimport withreaddirfromfs/promisesand added an internalfindPageFilesrecursive scanner insrc/app/api/dev/routes/route.tsthat collectspage.{tsx,js,jsx}files.apidirectories and entries that start with.or_, and uses aPAGE_EXTENSIONSset to detect page files.formatRouteLabel), duplicate removal, and sorting logic intact while switching topath.sepfor splitting directory segments.Testing
bun --bun install, but dependency fetches failed with registry403errors which prevented installingnextand other packages so a full build could not be run.bun --bun next build, which failed in this environment withScript not found "next"due to missing installed dependencies../node_modules/.bin/next build, which failed withNo such file or directorybecausenode_moduleswas not available in the current environment.Codex Task
Summary by Sourcery
Enhancements:
Confidence Score: 2/5
(marketing)will be kept in paths while normal segments may be removed, breaking the entire route discovery featureImportant Files Changed
Flowchart
flowchart TD A[GET /api/dev/routes] --> B{Check Dev Access} B -->|Denied| C[Return 401] B -->|Allowed| D[Get appDir path] D --> E[findPageFiles recursively] E --> F{For each entry} F --> G{Starts with . or _?} G -->|Yes| H[Skip entry] G -->|No| I{Is Directory?} I -->|Yes| J{Directory name == 'api'?} J -->|Yes| H J -->|No| K[Recurse into directory] K --> F I -->|No| L{Is File?} L -->|Yes| M{Filename == page.tsx/js/jsx?} M -->|Yes| N[Collect file path] M -->|No| H L -->|No| H N --> O[All files collected] O --> P[Map to routes] P --> Q[Remove dirname and extension] Q --> R[Split by path separator] R --> S[Filter route groups] S --> T[Build route path] T --> U[Add label and isDynamic flag] U --> V[Remove duplicates] V --> W[Sort routes] W --> X[Return JSON response]Last reviewed commit: 3ee9259