File Conventions

View as Markdown

Neutron uses a file-system based router. The files you create in src/routes directly determine the routes in your application.

Basic Mapping

| File Path | URL Path | | :--- | :--- | | src/routes/index.tsx | / | | src/routes/about.tsx | /about | | src/routes/blog/index.tsx | /blog | | src/routes/blog/post.tsx | /blog/post |

Special Files

  • index.tsx: The default route for a directory.
  • _layout.tsx: A layout component that wraps all child routes in its directory.
  • _error.tsx: An error boundary component that catches errors from child routes.
  • _*.tsx (any other file starting with _): Private files. These are ignored by the router. Use them for co-located components, utilities, or styles.

Outside src/routes/, two app-level files are picked up by convention: src/middleware.ts (global middleware) and src/server.entry.ts (mounted sub-apps, the WebSocket handler and other long-lived server objects; see Server entry).

Dynamic Routes

You can create dynamic route segments by wrapping the filename in brackets [].

| File Path | URL Path | Params | | :--- | :--- | :--- | | src/routes/users/[id].tsx | /users/123 | { id: "123" } | | src/routes/posts/[slug].tsx | /posts/hello-world | { slug: "hello-world" } |

Catch-all Routes

To match all remaining path segments, use the rest syntax [...].

| File Path | URL Path | Params | | :--- | :--- | :--- | | src/routes/docs/[...slug].tsx | /docs/guides/setup | { slug: "guides/setup" } | | src/routes/[...404].tsx | /any/unmatched/path | { 404: "any/unmatched/path" } |

Escaping

If you need a route to actually include brackets in the URL, you likely need to rethink your URL structure, as this is reserved syntax.

Resource Routes

A route module that exports HTTP method handlers instead of a component is served as an endpoint, not a page. This is how an SSR app carries JSON/REST/SSE endpoints in the same server and route table as its pages — no second process.

// src/routes/api/time.ts  →  GET /api/time
export async function GET() {
  return { now: new Date().toISOString() };
}

// src/routes/api/users/[id].ts  →  POST /api/users/123
export async function POST({ params, request }) {
  const body = await request.json();
  return Response.json({ id: params.id, body }, { status: 201 });
}

The rules:

  • Exports: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD — and no default component. A module with both is a build error ("exports both a default component and HTTP method handlers"): the page pipeline renders the component and would never dispatch the handlers, so the framework refuses the ambiguity.
  • Return values: a Response is answered verbatim; any other value is serialized as JSON (200). Throwing a ProblemError or a Response works like it does in loaders.
  • Method mismatch: a request whose method has no handler gets 405 as RFC 7807 application/problem+json with an Allow header. HEAD is implicitly supported wherever GET is.
  • Middleware: route and layout middleware exports, plus the global src/middleware.ts, run around every request in the usual order — so an endpoint can be auth-gated, including through a parent layout. Loaders never run for resource routes.
  • Never prerendered: a resource route produces no static file at build time; it answers per request even in mode: "static" (the default). Because nothing is prebuilt, gating it is legal — unlike a gated static page, there is no prerendered file that could bypass the gate.
  • Server-only: handler code is stripped from the client build and resource routes are excluded from the client route table entirely — nothing an endpoint exports ships to the browser.
  • OpenAPI: with server.openapi configured, each exported method appears in /openapi.json as an operation (operation id derived from method + path), with the route's path params. Request and response bodies are generic object placeholders — the spec is generated from the route table, which cannot see handler types — so supply real schemas per operation through the openapi.paths merge when you need them.

Need a whole Hono sub-app (e.g. tRPC) at one prefix instead of per-file routes? Use the server's mount option.