Skip to main content
kRouter
All posts
Fix an error

Request failed (404): the endpoint the page had always called

A dashboard page posted to an API route that did not exist — not in a release, not in git history. It compiled, it rendered, it was linked from the nav, and it had never once worked.

Kodelyth · The team behind kRouter
8 min read

The kRouter dashboard has a Chat page. Open it, pick a model, type a message, press enter, and:

Request failed (404)

Every time, for every model, on every install. The page was not broken by a regression. It had never worked.

The one-line cause

The page posts here:

const response = await fetch("/api/dashboard/chat/completions", {
  method: "POST",
  headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
  body: JSON.stringify({ model, messages, stream: true }),
});

That route did not exist. Not "was broken", not "was renamed" — there was no src/app/api/dashboard/ directory at all.

$ ls src/app/api/dashboard/chat/completions/route.js
No such file or directory
 
$ git log --diff-filter=ADR --all -- 'src/app/api/dashboard/**'
# (nothing)

The second command is the one worth sitting with. --diff-filter=ADR asks git for every commit that ever added, deleted or renamed anything under that path, across all branches. It returns nothing. The route was never created, never deleted, never moved. The frontend had been calling an address that had never had a building on it.

And exactly one file in the repository mentioned that path: the page doing the fetching. There was no second reference, which is why nothing ever flagged the mismatch — a dangling call looks identical to a correct one when only one side of it exists.

Why nothing caught it

Walk through everything that should have.

The page compiles. fetch() takes a string. No compiler, bundler or linter knows that a particular string is supposed to correspond to a file somewhere else in the tree.

The route list is correct. Every route that exists is registered and served. The build output is accurate — it just has one fewer entry than the frontend assumes, and nothing states that assumption anywhere.

The test suite is green. Nearly four thousand tests pass. None of them render the Chat page and press send, because doing that end-to-end would call a real provider and spend real tokens on every CI run.

The page is reachable. An earlier release added Chat to the navigation. That made a page that could not send a message considerably easier to find, which is the worst possible version of "progress".

Nothing was wrong with any single artifact. The caller and the callee were two strings that lived in different folders, and nobody had ever compared them.

The fix is boring, which is the point

The route now exists and is deliberately the same shape as the public one:

export const POST = withRedaction(async (request) => {
  await ensureInitialized();
  return await handleChat(await withServerApiKey(request));
});

Same handleChat, same redaction wrapper as /v1/chat/completions. The Chat page gets the same provider routing, the same failover, the same PII handling as every other client, because it is the same code path.

It is a separate route only because the two are authorised differently, and that distinction is the only interesting line in the file. /v1 is the public LLM API: it takes an API key. This one is dashboard surface — the deny-by-default gate has already required a session cookie before the request arrives, so the browser authenticates as itself and carries no key at all.

That creates one wrinkle. When the operator turns on requireApiKey, the shared handler starts demanding a key — and a browser has none to give. So the route borrows a stored active key server-side, rather than letting a global setting break the operator's own dashboard. This widens nothing: anything that can reach this route has already cleared the same gate as the endpoint that lists the keys themselves. And if there is no usable key, it forwards the request untouched so the handler returns its own 401, rather than inventing a second failure for one cause.

The test that should have existed

The fix is one file. The thing worth keeping is the check that would have caught it.

It walks every fetch("/api/...") in tracked frontend source and matches each one against the routes that actually exist:

103 frontend fetch targets
154 registered routes
unresolved: 0

Matching follows Next's own rule — whole paths against the route set, [seg] covering one segment and [...seg] covering the rest.

It is deliberately not a filesystem walk, and that distinction cost a false alarm before it was fixed. A naive per-segment descent reports /api/oauth/codex/stop-proxy as missing, because a literal codex/ directory sits next to the [provider]/[action] route that actually serves it, and a greedy walk descends into the literal one and gives up. Next does not route that way: it matches complete paths against the registered set, so there is no static directory "shadowing" a dynamic sibling for a path the static branch never claimed.

That was checked against the build's own app-path-routes-manifest.json rather than reasoned about, which is the only reason the second "bug" was correctly thrown away instead of reported.

Both counts carry a floor. A scan that quietly stops seeing files reports no problems at all, which is the exact failure this kind of check keeps producing — so if the route count or the fetch count collapses, the test fails instead of passing in silence.

Planting the original bug — pointing the page at a route nobody wrote — now fails with the path and the file that calls it:

the frontend fetches these and nothing serves them, so they 404 at runtime
+ "/api/dashboard/chat/nope  (fetched in .../BasicChatPageClient.js)"

The general shape

This is the same animal as the two API routes that never shipped, where a build filter deleted directories named logs and took /api/usage/logs with them — an endpoint that existed in source, compiled every time, and had never once been in a published package.

Both are a gap between artifacts. There, the route existed in the build and not in the package. Here, it existed in the frontend's imagination and nowhere else. In both cases every individual check was green, because every check was looking at one artifact and the defect lived in the space between two.

The habit that catches this class is dull and mechanical: when two things have to agree, write the thing that compares them. Not a test that exercises one side and trusts the other — a test whose entire job is the comparison. Most of them find nothing for years. This one found a page that had never worked.

If you route Chat through kRouter's dashboard, this landed in 0.5.196.

Kodelyth · The team behind kRouter

Published by Kodelyth, the team that builds kRouter. Posts are drafted with AI assistance and reviewed by a person before they go out. kRouter is free and MIT licensed.

Install kRouter