TanStack / TanStack/router

API routes without GET handlers doesn't return a 404 status code

Open
#5,473 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

API routes without a GET handler doesn't return a 404 status code by default but return 200 OK, with an error in the console:

{
    "data": {
        "name": "Error",
        "message": "Invariant failed: Expected to find a match below the root match in SPA mode.",
        "stack": "Error: Invariant failed: Expected to find a match below the root match in SPA mode.\n    at invariant (http://localhost:3000/node_modules/.pnpm/tiny-invariant@1.3.3/node_modules/tiny-invariant/dist/esm/tiny-invariant.js?v=c5ed1ad0:12:9)\n    at hydrate (http://localhost:3000/node_modules/.pnpm/@tanstack+router-core@1.132.47/node_modules/@tanstack/router-core/dist/esm/ssr/ssr-client.js?v=c5ed1ad0:141:5)\n    at async hydrateStart (http://localhost:3000/node_modules/.pnpm/@tanstack+start-client-core@1.132.54/node_modules/@tanstack/start-client-core/dist/esm/client/hydrateStart.js?t=1760429159805&v=c5ed1ad0:29:5)"
    },
    "__isServerError": true
}

Route API without a GET handler:

import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/api/test-no-get')({
  server: {
    handlers: {
      POST: () => {
        return new Response('ok', { status: 200 });
      },
    },
  },
});
Your Example Website or App

https://stackblitz.com/edit/github-zlbrcubx-vwhcvwvk?file=src%2Froutes%2Fapi%2Ftest-no-get.ts

Steps to Reproduce the Bug or Issue
  1. Create an API route without a GET handler.
  2. Access the API.
  3. Check the console and status code.
Expected behavior

It should return a 404 status code to avoid confusing search engines.

Screenshots or Videos

No response

Platform
  • Router / Start Version: 1.132.47 / 1.132.56
  • OS: Windows
  • Browser: Chrome
  • Browser Version: 141.0.7390.77
  • Bundler: Vite
  • Bundler Version: 7.1.9
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction route shown in src/routes/api/test-no-get.ts from the linked StackBlitz example, then trace how the router handles an API request when only a POST handler exists. Reproduce the 200 response and hydration error, and verify that an API route without GET returns 404 without the reported client error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.