Routing
One file, routes.tsx, maps paths to components. Routes nest: a route with children is a layout.
Map paths to components
defineRoutes checks the tree and returns it. These are the pokedex example's routes:
import { defineRoutes } from '@brust/core/routes'
import AppLayout from './components/AppLayout'
import BrowsePage from './pages/BrowsePage'
import DetailPage from './pages/DetailPage'
import HomePage from './pages/HomePage'
import NotFoundPage from './pages/NotFoundPage'
export const routes = defineRoutes([
{
Component: AppLayout,
children: [
{ path: '/', Component: HomePage },
{ path: '/pokedex', Component: BrowsePage },
{ path: '/pokemon/{name}', Component: DetailPage },
],
},
{ path: '*', Component: NotFoundPage },
])A route has five fields, and only these five: anything else fails the build.
interface Route {
path?: string // '/pokemon/{name}', or '*' at the root
Component?: ComponentType
loader?: (ctx) => data // see Loaders and params
cache?: RouteCacheConfig // see Caching
children?: Route[] // makes this route a layout
}Try a path
The matcher below runs the routes above in the browser: type a path and see which route answers it, with the decoded params.
- Route
- /pokemon/{name}
- Params
- name = "mr mime"
- Status
- 200
Params and the catch-all
- A segment in braces,
{name}, is a param. The loader gets it decoded:%20becomes a space and%2Fbecomes/, while+stays a plus. A value with a broken%sequence is passed raw. '*'is the catch-all. It is allowed once, at the root, and it renders with status 404. Without one, an unknown path gets a plain 404.- A segment
{*rest}at the end of a pattern takes the rest of the path, at any depth:/docs/{*rest}answers/docs/aand/docs/a/b/cwithparams.restset to"a/b/c", decoded like any other param. Unlike'*'it is an ordinary route: status 200, and acacheon the route, with itsprefix, applies. It needs at least one character after the prefix, so/docsand/docs/do not match it: add a/docsroute if they should answer. - A
{*rest}and a{slug}at the same level conflict, whatever order they are written in: the router holds only one of them, and the build fails withroute-conflict, naming both routes. Put the param route under a longer literal prefix, or drop one. - Two routes that differ only in a param name, such as
/a/{id}and/a/{slug}, are the same route: the build fails withduplicate-route.
A path that ends in a slash
By default a route matches its path as written: /cart/ is not /cart (404), and /docs/{*rest} takes /docs/a/ with params.rest ending in /, a second URL of the page. Set [server] trailing_slash = "redirect" (one URL per page, 308 to the slashless path) or "ignore" (both spellings answer); see Configuration.
Paths beyond ASCII
A literal path may contain any character: { path: '/café' } matches what a browser sends, /caf%C3%A9. The build stores the pattern in that encoded form and normalises every request path the same way (lowercase hex is uppercased, raw UTF-8 is encoded) before matching, so /café, /caf%c3%a9 and /caf%C3%A9 are one route and one cache entry. The path is never decoded before matching: %2F inside a param stays a literal slash in the param's value, and a loader still gets the decoded value in params.
Layouts and Outlet
A route with children is a layout. It can leave out path, which adds no prefix. Its component renders <Outlet/> where the matched child goes. The outermost layout returns the whole document.
import { Outlet } from '@brust/core/routes'
export default function AppLayout() {
return (
<html lang="en">
<head>
<title>Pokédex</title>
</head>
<body>
<nav><a href="/pokedex">Pokédex</a></nav>
<main>
<Outlet />
</main>
</body>
</html>
)
}A layout must stay static or native. <Outlet/> inside a component that compiles to a React island fails with outlet-in-react, and an Outlet in a component that is not a layout route fails with outlet-outside-layout.
A page's own title
A <title>, <meta> or <link> that a page renders in its body is moved into <head> in the HTML brust writes, as React 19 does: after the head's own children, in render order, with no copy left in the body. The same holds for brust build --target static. An SVG <title>, an element with itemProp and a stylesheet link stay where they are. Brust does not remove duplicates: if the layout's head has a <title>, the page's follows it and the document has both, as with React. Leave the title out of the layout's head to give each page its own.
What the build checks
| Rule | When |
|---|---|
route-config | An unknown field, a leaf without path or Component, a bad cache. |
duplicate-route | Two routes match the same paths. |
route-conflict | The server’s router refuses the routes together, such as /{*rest} next to /{slug}. The message is the router’s own and names both route ids; without this check the app would fail at brust start. |
nested-catch-all | '*' below the root. |
duplicate-catch-all | More than one '*'. |
route-outside-base-path | With [server] base_path, a route whose pattern is not under it (the ids and patterns are listed). |
An app mounted under a path (Mount under a path) writes its routes as the real URLs: '/portal', '/portal/orders/{id}'. Nest them under one { path: '/portal', children } parent to write the base once. Links, navigate() and redirect() take the full path too, so nothing is prefixed behind your back. The root '*' then answers anything under the base that no route matched, with a 404.