Skip to content

Routing

In Kiln, the file tree is the route table. Each file under app/pages becomes a URL, and you never write a list of routes. This guide explains how file names turn into URLs, how dynamic segments work, how route groups affect layouts without changing URLs, and how Kiln decides which route wins when two could match.

From files to URLs

The path of a file, relative to app/pages, without its extension, becomes the URL path:

FileURL
app/pages/index.tsx/
app/pages/about.tsx/about
app/pages/blog/index.tsx/blog
app/pages/blog/launch.tsx/blog/launch
app/pages/docs/guide.md/docs/guide

The name index maps to its folder. So blog/index.tsx and blog.tsx both produce /blog, but you should use one form per URL. Two files that map to the same URL cause a build error: Route conflict: ….

Dynamic segments

Wrap a segment name in square brackets to capture it:

text
app/pages/blog/[slug].tsx       ->  /blog/:slug
app/pages/users/[id]/posts.tsx  ->  /users/:id/posts

The captured value arrives in two places: the params object passed to the loader and action, and the params prop passed to the page component.

tsx
// app/pages/users/[id].tsx
export async function loader({ params }: { params: Record<string, string> }) {
  return { id: params.id };
}

export default function User({ params, data }: { params: Record<string, string>; data: { id: string } }) {
  return <h1>User {data.id}</h1>;
}

Captured values are decoded from the URL, so /blog/hello%20world gives params.slug as hello world.

Catch-all segments

Wrap a name in [...name] to capture the rest of the URL:

text
app/pages/docs/[...path].tsx  ->  /docs/*path

/docs/a/b/c gives params.path the value a/b/c. A catch-all matches one or more segments. It does not match /docs on its own, so add docs/index.tsx if you want that URL too.

A catch-all must be the last segment of the file name. Kiln treats it as the final part of the route.

Route groups

Wrap a folder name in parentheses to group pages without adding a URL segment:

text
app/pages/(marketing)/_layout.tsx   a layout for the group
app/pages/(marketing)/pricing.tsx   ->  /pricing
app/pages/(marketing)/about.tsx     ->  /about

A route group is useful for giving a set of pages their own layout. It never changes the URL. Groups can nest, and each group's _layout applies to the pages inside it. See Layouts.

Priority

Routes are sorted once, when the table is built. Kiln compares routes segment by segment. For each segment position:

  1. A static segment beats a dynamic segment.
  2. A dynamic segment beats a catch-all.
  3. A route that ends beats a route that continues.

So with these files:

text
app/pages/blog/new.tsx
app/pages/blog/[slug].tsx
app/pages/[...rest].tsx

the URL /blog/new goes to new.tsx, /blog/hello goes to [slug].tsx, and /blog/a/b goes to [...rest].tsx.

The matcher checks routes in this sorted order and returns the first match. You do not need to think about the order in which files were created.

Special files

Files whose names begin with an underscore are never routes. Kiln uses these names:

NamePurpose
_layout.tsxLayout for the folder and everything below it. See Layouts.
_error.tsxPage shown when a page throws. See Errors.
_not-found.tsxPage shown when no route matches. See Errors.

Render mode suffixes

A .server or .client suffix before the extension sets the render mode and is removed from the URL:

text
app/pages/about.server.tsx      ->  /about, rendered on the server only
app/pages/dashboard.client.tsx  ->  /dashboard, rendered in the browser only

See Rendering modes.

Matching rules

  • Matching uses the URL path only. The query string is ignored by the route match. Loaders can read it from request.url.
  • Trailing slashes do not matter. /about and /about/ reach the same page.
  • Matching is case-sensitive. /About does not reach about.tsx.
  • A HEAD request renders the page and returns no body.

Checking your routes

Print the route table with the CLI:

bash
bunx kiln routes

Each line shows the URL pattern, the render mode, and the file. Use it to confirm how a new file maps before you rely on it.

Route conflicts

If two files map to the same pattern, the scanner stops with an error that names both files:

text
Route conflict: blog/index.tsx and blog.tsx both map to /blog

Rename or move one of them. API routes have the same rule, under api/.