Skip to content

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

@kcoet

Description

@kcoet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions