In the Next.js App Router, what does prefixing a folder inside app/ with an underscore — for example app/_components/ — do, and how would you serve a URL segment that genuinely starts with an underscore?
answer
- one character opts a subtree out
- the exclusion applies all the way down
- colocation was already safe — this is a guarantee
- percent-encode it to get it back
- not the same as parenthesised folders
basics
~20 sAn underscore marks a private folder: Next excludes it and everything nested inside it from routing, so even a page file within it produces no URL. To serve a URL segment that really starts with an underscore, name the folder with the URL-encoded form %5F instead.
solid answer
~40 sA leading underscore makes the folder **private**. Next takes it out of the routing system entirely, and the exclusion is recursive — nothing beneath `app/_components/` can become a URL, not even a `page.tsx`. It is an explicit opt-out rather than a necessity, since colocation already works: an ordinary file in a route folder is ignored by routing anyway. What the underscore adds is a guarantee at the folder level — a clear signal to readers that the directory holds implementation, protection against someone later dropping a page file in and accidentally publishing a route, and a namespace safely separated from future Next file conventions. If you actually need a URL segment beginning with an underscore, name the folder with the URL-encoded underscore, `%5F`, so `app/%5Fdrafts/` serves `/_drafts`.
go deeper
Know that a leading underscore marks a folder as private, that nothing inside it becomes a URL, and that it is a common home for shared components under app/.
Explain that the exclusion is recursive — even a page file inside is not routed — and that it is an explicit opt-out rather than a necessity, since colocation already keeps non-reserved filenames out of routing. Know the %5F escape.
Judge when the guarantee is worth the extra directory: shared code across segments, large areas where a stray reserved filename is a real risk, or naming that tells a newcomer nothing inside is routable.
Own the directory conventions across the codebase — where shared internals live relative to app/, how private and parenthesised folders are used consistently, and how reviewers catch a route silently disappearing under a private parent.
## What the underscore does Prefix any folder under `app/` with `_` and Next treats it as **private**: it is excluded from routing, and so is everything nested inside it. The recursion is the part worth stating out loud in an interview — a `page.tsx` deep inside a private folder still produces no URL, because the exclusion applies at the private folder and everything below it is unreachable regardless of what conventions it contains. ``` app/ _components/ Chart.tsx never routable nested/ page.tsx still never routable dashboard/ page.tsx -> /dashboard ``` ## Why bother, when colocation already works? This is the follow-up that separates a memorised answer from an understood one. The App Router routes only on a fixed set of reserved filenames, so an ordinary module dropped into a route folder is already invisible to routing. You do not *need* a private folder to keep a component next to its page. What the underscore adds is a guarantee scoped to a directory rather than to individual files: - **Intent.** The name says "implementation, not a route" to every reader and reviewer, without them having to know the reserved-filename list. - **Protection against future mistakes.** Colocation is safe only while nobody adds a reserved filename. Someone creating `page.tsx` inside a shared folder to sketch something out publishes a route by accident. Inside a private folder they cannot. - **Insulation from the convention namespace.** Reserved filenames are Next's namespace and it has grown over time. A private folder is a subtree the routing layer never inspects. - **Grouping unrelated to a single route.** Colocation naturally holds code belonging to *this* segment. A private folder is a clean home for utilities shared by several segments while still living under `app/`. The common patterns are a private folder near the root for cross-cutting internals — `app/_lib/`, `app/_components/` — and private folders inside a feature area for code shared by its child segments. ## Serving a literal leading underscore Occasionally a URL genuinely needs to begin with an underscore. Since a raw `_` prefix means "private", the escape hatch is the URL-encoded form: name the folder `%5F` followed by the rest of the segment. ``` app/ %5Fdrafts/ page.tsx -> /_drafts ``` `%5F` is the percent-encoding of `_`, and Next decodes it when building the route, producing a public segment whose URL starts with an underscore. This is niche, but it is exactly the kind of detail an interviewer uses to check whether you have read the conventions or only pattern-matched them. ## How this differs from the other folder-name convention Do not confuse a private folder with a folder whose name is wrapped in parentheses. Both are folder-name conventions and both change routing, but in opposite directions: - **Private folder, `_name`:** the subtree leaves routing altogether. Nothing under it is reachable. - **Parenthesised folder, `(name)`:** the subtree stays fully routable; the folder simply contributes no segment to the URL, so it organises files without changing paths. One removes routes, the other removes a path segment. Mixing them up in an interview reads as unfamiliarity with the directory conventions generally. ## Practical guidance Do not sprinkle private folders everywhere. A route's own component sitting beside its `page.tsx` is idiomatic and already safe; wrapping every helper in `_` adds a directory level for nothing. Reach for the underscore when a directory is shared, when it is large enough that a stray reserved filename is a realistic risk, or when you want the name itself to tell a newcomer that nothing inside is a route. And remember the recursion — moving a genuine route folder underneath a private one silently removes it from the application, which is a confusing bug to chase precisely because the code still compiles and the files look right.
- If colocation already keeps helper files out of the routing table, what does a private folder actually add?A directory-level guarantee rather than a per-file accident of naming. It states intent in the name, stops someone later adding a reserved filename and publishing a route by mistake, keeps a subtree out of Next's growing convention namespace, and gives code shared by several segments a home under `app/`.
- What happens to a page.tsx placed several folders deep inside a private folder?Nothing routable. The exclusion is recursive: once a folder is private, no descendant can produce a URL regardless of which reserved filenames it contains. This also makes it a real hazard — moving a genuine route folder under a private one removes it from the application while the code still compiles.
- How does a private folder differ from a parenthesised folder name?They pull in opposite directions. A private `_name` folder leaves routing entirely, so nothing beneath it is reachable. A parenthesised `(name)` folder stays fully routable and only stops contributing a segment to the URL, letting you group routes without changing their paths.
saying these in an interview costs you the question
- Thinks helper files must be in a private folder to avoid becoming routes
- Believes a page file inside a private folder still creates a route
- Confuses private folders with parenthesised folder names
- Assumes the exclusion applies only to the folder's direct children
- Says a leading underscore in a URL is impossible in the App Router