Routing

Routing

A file under src/routes/ is a route, and its path is its URL. There is no route table to keep in step with the files.

Files become URLs

FileURL
routes/index.tsx/
routes/about.tsx/about
routes/blog/index.tsx/blog
routes/blog/[slug].tsx/blog/hello, /blog/anything
routes/files/[...path].tsx/files/a, /files/a/b/c
routes/shop/[[...filter]].tsx/shop, /shop/red/large
routes/guide.md/guide — a Markdown page

A route file default-exports a component, written in .tsx or .jsx — or it is a .md file, which the build turns into one.

Two files that would serve the same URL — about.tsx and about/index.tsx, or about.tsx and about.md — fail the build and name both files.

Files that are not routes

  • A name that starts with _ — _helpers.tsx, _draft.md — is private: it can be imported, but it has no URL.
  • layout.tsx, error.tsx and loading.tsx are convention files, described below.
  • Anything that is not a component — a .css file, a .json file — is not a route. A stylesheet placed here is scoped to its directory; see Styling.

Segments

A [name] in a file or directory name captures that part of the path. The route's component receives the captured values as params:

// routes/room/[id].tsx — /room/lobby
export default function Room({ params }) {
  return <h1>#{params.id}</h1>;
}

[...name] captures the rest of the path, one segment or more. [[...name]] does the same, and also matches with nothing after it.

Important

A captured rest of the path arrives as one string, joined with / — /files/a/b/c gives params.path === "a/b/c", not an array. An optional one that matched nothing is absent: /shop gives {}.

Values are strings, percent-decoded. /post/7 gives params.id === "7", and /post/caf%C3%A9 gives params.id === "café". The path is split on its /s before anything is decoded, so %2F never starts a new segment: /post/a%2Fb matches [id] and gives params.id === "a/b". In a captured rest of the path it decodes to / as well, so /files/a%2Fb and /files/a/b both give "a/b". Build a link with encodeURIComponent(value) and the page receives value unchanged.

A decoded value can hold any character, < and / included. Rendered as text or as an attribute it is escaped, like any other string. A segment that does not decode to UTF-8, such as %FF, is answered with 400 and names the param.

Layouts

A layout.tsx wraps every route in its directory and below it. The layout marks where the page goes with <children />:

// routes/blog/layout.tsx — wraps /blog and /blog/*
export default function BlogLayout() {
  return (
    <div className="blog">
      <nav>…</nav>
      <children />
    </div>
  );
}

Layouts nest from the outside in: a page under routes/blog/ is wrapped by routes/layout.tsx, then routes/blog/layout.tsx, then placed.

A layout is rendered once per route at build time, and receives that route as route — the pattern, not the visited URL, so every /blog/[slug] page shares "/blog/[slug]". That is enough to mark the current page in a navigation with no JavaScript:

export default function DocsLayout({ route }) {
  return (
    <nav>
      <a href="/docs/routing" aria-current={route === "/docs/routing" ? "page" : undefined}>
        Routing
      </a>
    </nav>
  );
}

Because a layout is rendered at build time, it has no request: it cannot read user, or the URL beyond its route. This site's docs are a layout of that kind — header, sidebar and previous/next links, all decided at build time — around Markdown pages.

Boundaries

error.tsx and loading.tsx follow the same reach as layouts — a file covers its directory and everything below it, and the nearest one wins. They stand in for an island (a Tier C component): error.tsx when its server render fails, loading.tsx while it is still resolving.

Page title and description

A route sets its <title> and description with an exported object:

export const metadata = {
  title: "Pricing",
  description: "What it costs, and what it does not.",
};

A Markdown page sets the same two values in its frontmatter. A route that needs a value from the request exports generateMetadata instead of metadata.

Signed-in routes

With an auth block in the config, one line in a route gates it:

export const auth = "required";

A visitor without a session is sent to the sign-in route the config names (login). The check runs on the server before the route renders, so the page never reaches someone it was not for.

A URL that matches nothing

It is answered with 404. A browser gets a page: your own, if the app has src/routes/404.tsx (rendered like any other page and served with status 404), or a plain built-in one. A client that did not ask for HTML gets a JSON error.

Next: Markdown pages.