Deviopath › Next.js

Detect Route Build Errors

Hard · Routing

Some folder structures make next build fail because Next.js cannot tell which page a URL belongs to. Write findRouteConflicts(files) that detects four of these errors. Only page.<ext> files under app/ count; _private folders (and everything inside) are ignored; route group folders (name) are removed from the URL.

- Two or more pages with the same URL pattern (for example through different route groups): Conflicting pages resolve to "<url>": <files sorted, joined with ", ">. The URL is the path of folders without groups, written as-is (/blog/[slug]), and / for the root.

- Different parameter names at the same dynamic position. Positions are compared by the folders before them, where each dynamic folder counts as a placeholder of its kind ([a] and [b] are the same position). Dynamic, catch-all and optional catch-all folders are checked separately (a [id] next to a [...rest] is fine). Message, with the distinct names sorted: You cannot use different slug names for the same dynamic path ('a' !== 'b'). For three names: ('a' !== 'b' !== 'c').

- A catch-all and an optional catch-all at the same position: You cannot use both a required and optional catch-all route at the same level ("[...<name>]" and "[[...<name>]]"). (use the alphabetically first name of each kind).

- The same parameter name twice in one page path (any kind): You cannot have the same slug name "<name>" repeat within a single dynamic path: <url>.

Return the unique messages sorted with sort().

Next: Sliding-Window Rate Limiter in proxy.ts