Your Next.js route 404s because the folder name starts with an underscore

Most bugs are hard because the logic is hard. This one was hard because one character in a folder name meant something I didn’t know it meant.
My logger ships a route handler. The browser collects logs, posts them to your server, and they show up in your terminal instead of dying in a console nobody has open. For that to work, your app needs one file. The one I documented and shipped as the default was this:
app/api/__log/route.tsTwo underscores, because that is how I mark things as “internal, don’t call this yourself.” It looked tidy. It was dead on arrival.
What the underscore does in the App Router
In the Next.js App Router, a folder whose name starts with _ is a private
folder. Next.js takes it, and every folder inside it, out of routing
completely. This is documented
behaviour,
not a bug. It exists so you can keep components, helpers and utilities inside
app/ without Next.js trying to turn each folder into a URL.
So app/blog/_components/Post.tsx is not a route. Good. And
app/api/__log/route.ts is not a route either. Not good, because I was
telling people to create exactly that file.
POST /api/__log did not throw. It did not warn at build time. It returned the
normal 404 page, the same page you get for any URL that was never a route.
Why nobody noticed for so long
Three things stacked up.
A 404 on a POST is boring. You see one in the network tab and your brain files it under “something upstream, probably fine.” Nothing about it says “half of your logging pipeline does not exist.”
The logger could not log its own failure. The whole point of the package is to carry errors somewhere you can see them. The one error it could not carry was the one that broke the carrying.
There was a fallback, and the fallback worked. When the fetch transport fails, the logger falls back to a Server Action. The Server Action goes through the Next.js router on every call, so it is slower and noisier. Because the fetch path was never alive, the fallback quietly carried 100% of client logs for everyone on the default config. Then the fallback started producing React warnings about updating a component while rendering another one, and I spent real time blaming the fallback. The fallback was fine. It was just doing a job alone that it was never meant to do alone.
That is the shape of this bug: the symptom shows up three steps away from the cause, and nothing in between points backwards.
See it for yourself
You do not have to take my word for it. In any App Router project, create two files:
// app/api/ping/route.ts
export function GET() {
return Response.json({ ok: true })
}// app/api/_ping/route.ts
export function GET() {
return Response.json({ ok: true })
}Start the dev server and hit both:
curl -i localhost:3000/api/ping
curl -i localhost:3000/api/_pingThe first one answers with your JSON. The second one gets the 404 page, because that route was never registered. Same file, same export, one character of difference in the folder name.
Fix one: catch it at setup time
The default moved to /api/log-relay. No underscore, nothing for the router to
skip.
But the default is not the only way in. Anyone can pass a custom relayUrl and
walk straight back into it. So the provider checks the path it was handed, once,
when it mounts:
function warnIfUnroutableRelayUrl(relayUrl: string): void {
if (process.env.NODE_ENV !== "development") return
const offending = relayUrl
.split("/")
.filter(Boolean)
.find((segment) => segment.startsWith("_"))
if (!offending) return
console.warn(
`[logger] relayUrl "${relayUrl}" contains the segment "${offending}", which ` +
"starts with an underscore. Next.js treats underscore-prefixed folders as " +
"private and excludes them from routing, so a route handler there is " +
"unreachable and the relay will fall back to the slower Server Action " +
"transport. Use a segment without a leading underscore (the default is " +
"'/api/log-relay'), or name the folder with the %5F escape.",
)
}It splits the path, finds the first segment starting with _, and says what
the actual rule is. Development only. This is the kind of message you want
shouting at you the first time you run the app locally, not once an hour in a
production log.
Fix two: catch it at request time
The setup check only knows about one cause: an underscore you typed. It cannot help the much more common case, which is that you simply never created the route file at all.
So the fetch transport handles the 404 too:
// The route handler isn't mounted at all — a setup problem, not a
// transient one. Distinct from a rejection so the caller can switch
// transports permanently instead of retrying.
if (res.status === 404) return 'missing'A 404 here is not a flaky network. Retrying it with backoff is just a slower
way to fail, so missing is its own outcome: stop using this transport, move
to the fallback, and say so once.
function warnRouteHandlerMissing(opts: ClientTransportOptions): void {
if (warnedRouteMissing) return
warnedRouteMissing = true
if (process.env.NODE_ENV !== 'development') return
console.warn(
`[logger] No route handler found at ${opts.relayUrl} (404). Falling back ` +
'to the relay Server Action, which dispatches through the Next.js ' +
'router on every call and is therefore slower and noisier.\n' +
'Create app/api/log-relay/route.ts:\n\n' +
" export { POST } from '@developerehsan/nextjs-logger/relay/route-handler';\n",
)
}Note what this message does not do. It does not claim the underscore is your problem. From the browser’s side, a 404 from a private folder, a 404 from a missing file, and a 404 from a proxy sitting in front of your app are the same response. The transport cannot tell them apart, so it says the one true thing it knows — nothing is answering at this URL — and tells you the file to create. The setup check is the one that can name the underscore, because it runs where the configured path is still visible as text.
Two checks, two different moments, because the information you need is only available at one of them each.
Other naming rules in the same family
While reading the routing conventions properly, instead of one bug at a time:
- Private folders apply at any depth.
app/api/_internal/hooks/route.tsis just as unreachable asapp/api/_internal/route.ts, because the exclusion covers the whole subtree. - Folders in parentheses, like
app/(marketing)/, are route groups. They organise files without adding anything to the URL. That is a feature, but it surprises people reading a folder tree and expecting the path to match. - If you genuinely want a URL segment that starts with an underscore, the
escape is
%5F, the URL-encoded underscore. A folder named%5Flogserves/_log. It is in the docs under the same section, and I would still rename the folder instead.
The bit that generalises
An underscore meant “private, hands off” to me. It meant “exclude from routing” to Next.js. Both conventions are reasonable on their own. Where they overlap, you get a feature that never runs, with no stack trace, no lint error and no loud 404 — just silence.
Before you bake a folder name into a default that other people will copy, spend
thirty seconds checking whether your framework already has an opinion about that
character. Underscores, dots, parentheses and @ all mean something somewhere.
If you want the rest of how those logs get from the browser to your terminal, that is in how client logs reach your terminal without a public endpoint.