Layouts
layout.tsx and Layout, nesting, tifa.Use, breadcrumbs and the current page, layout_.tsx, instead of route groups
The Layout of a folder
A Layout lives in its folder next to the page of that folder, if there is one: the Layout function in route.go and layout.tsx wrap the page in the same folder and every page below it. layout.tsx places child routes with <Outlet />. Either may be left out: without layout.tsx the child routes render directly, and without a Layout function the Layout has no data. A Layout with no page in its folder or below it fails the build.
package project
func Layout(c *tifa.Ctx) (projects.Project, error) {
return projects.GetProject(c.Param("project"))
}
Layoutgoes inroute.go, next toLoad,SSR,Memo,Cacheand the method handlers of the same folder. Declaring it in any other file of the package, alayout.gofor example, fails the build. Aroute.gowith only aLayoutis no API route.- A Layout that reads the query string takes the parameters as its second parameter, as a
Loaddoes:func Layout(c *tifa.Ctx, q Filter) (T, error)(see Query parameters). A name that a Layout and a page below it declare, or two Layouts of one chain, must have the same type in each, or the build fails and names the two places. A Layout that takes none takes only the Ctx. - A Layout reads the path parameters of its own folder:
Layoutinapp/projects/project+/route.gomay readproject, notorder. A parameter of a folder below it reads as"", inc.Paramand inc.Request(), since the data of the Layout stays while the visitor moves between the pages below it. The build fails where it sees such a name as a constant. A page'sLoadreads the parameters of its own path, and the build checks a constant name the same way. - When you switch between pages under the same Layout, the Layout component is not remounted and keeps its state. When a path parameter of its folder or of a folder above it changes (for example, you move from
/projects/ato/projects/bunderapp/projects/projectId+/), the Layout remounts and its state resets, whether or not itsLayoutfunction reads the parameter: the path names the thing the page shows, so a draft, an open panel or the values of a form never carry over to another project. Pages work the same way, those without aLoadtoo, as in Next.js. Keep what should outlive such a move, such as the scroll position of a list next to the details of an item, in a Layout above the folder of the parameter. When a query parameter changes, even one it reads, the Layout loads its data again and renders it, but is not remounted and keeps its state, as TanStack Router does by default. So a filter keeps the view the visitor chose. State a component set up from the data ofLoad, such as withuseState(data.x), is not reset by a new query either. Where it should follow the new data, give the component akeyfrom the value, or update the state in an effect. Pages work the same way. - Pages and Layouts can call
tifa.Use(c, project.Layout)to get the result of a Layout that wraps them, for the current request: the Layout of the same folder or of an ancestor folder, unless alayout_.tsxbetween drops it. The sameLayoutruns only once per request. Passing any other Layout, or anything but a package-levelLayoutfunction, such as a closure, fails the build where it sees it. ALayoutfunction cannot use its own Layout, directly or through a helper. A Layout with query parameters cannot be passed totifa.Useyet, and the build says so: when otherLoadfunctions need the result of a Layout, give that Layout no query parameters, and read them in theLoadof the page. - On navigation in the browser, the navigation data request runs only the
LoadandLayoutfunctions of the listed routes. If an ancestor Layout's data is still in the cache, that Layout is not in the list and itsLayoutdoes not run this time. So do not put permission checks only in a Layout. Put them inOnRequest, or have the page'sLoaddepend on the checking Layout throughtifa.Use, so the check runs on every request. - The root Layout also holds the
documentHeadof the site (see The document shell and documentHead).
Breadcrumbs and the current page
A Layout often shows something that follows the page below it: breadcrumbs, the current entry of a menu, the name of a section in the header. Read the address for it in layout.tsx, not in the Layout function. The data of a Layout is not loaded again when the visitor moves between the pages below it, so a path that Layout read would stay the one of the first page. For that reason c.Path() in a Layout, or in a function it calls, fails the build.
The router has the address, and the component renders again when it changes:
// app/projects/project+/layout.tsx
import { Link, Outlet, useRouterState } from '@tanstack/react-router'
import { layout } from './route.gen'
const sections: Record<string, string> = { orders: 'Orders', settings: 'Settings' }
export default function Layout() {
const project = layout.useLoaderData()
// /projects/studio/orders/42 is in the section "orders"
const section = useRouterState({ select: (s) => sections[s.location.pathname.split('/')[3] ?? ''] })
return (
<>
<nav aria-label="Breadcrumb">
<Link to="/projects">Projects</Link>
{' / '}
<Link to="/projects/$project" params={{ project: project.id }}>
{project.name}
</Link>
{section && ` / ${section}`}
</nav>
<Outlet />
</>
)
}
selectreturns only what the component needs, so it renders again only when that changes.- Names and titles, such as the name of the project here, still come from data: the data of the Layout, or the data of the page, which the page can show in its own part of the frame.
- A
Linkknows when it points at the current page: itsactivePropsstyle the current entry of a menu without reading the address yourself.
A Layout without the Layouts above
Name the component file layout_.tsx instead of layout.tsx, and the Layout of the folder shows its pages without any Layout above it, the root Layout included. Only the document shell the framework writes stays around them. The storefront of examples/shop works this way: it draws its own header and footer, and none of the workbench the root Layout draws around the other pages.
app/
route.go layout.tsx the workbench: sidebar, top bar, the list of shops
projects/
project+/
route.go layout.tsx page.tsx inside the workbench
store/
layout_.tsx the storefront starts here, without the workbench
_locales/ zh-CN.json and en-US.json
route.go page.tsx the storefront
cart/
route.go page.tsx inside the storefront
- The pages below it, also those further down, do not render the Layouts above it, and their
Layoutfunctions do not run for them: not for a page, not for the navigation data request of the browser, not for the 404 of a URL below it that no page has. So their data never reaches these pages. - The
Layoutin theroute.goof the same folder gives it data as usual. The folders below it nest as usual: alayout.tsxfurther down shows inside it and itsLayoutruns. Alayout_.tsxfurther down drops it in turn, with everything above it. - What belongs to the document still applies: the root Layout's
documentHeadwith itshtmlAttributes, andOnRequest. What belongs to the Layouts above does not: theirhead, and theirErrorBoundary. When theLayoutof alayout_.tsxfails, or returnstifa.ErrNotFound, no Layout is above it to show the error, so the framework's default page shows. Its ownErrorBoundaryshows for what fails below it, as with any Layout. To show your own page when thatLayoutfails, put thelayout_.tsxone folder higher, with an<Outlet />and anErrorBoundary. - In the browser, a move between a page below it and a page outside it swaps the whole frame, and the Layouts of the new page load their data when the router has none of it yet.
- The build fails for a
layout_.tsxinapp/, which has no Layout above it, forlayout.tsxandlayout_.tsxin the same folder, and fortifa.Useof a Layout that alayout_.tsxdrops, from a page or Layout below it. It also fails for a Layout whose pages are all below alayout_.tsx, since it wraps none of them. - React Router and TanStack Router also end a name with
_to keep a route out of a Layout, but only out of the Layout of the segment before it: the root route and the Layouts further up still wrap it. Tifa'slayout_.tsxdrops every Layout above it, the root Layout too.
Instead of route groups
A route group is a folder that gives the pages in it a Layout and adds nothing to the URL: (shop) in Next.js, SvelteKit and Nuxt, a _name route in TanStack Router, a layout route without a path in React Router. Tifa has no route groups and will not add them. This section says what to write for each thing a route group is used for, and why Tifa leaves them out.
In Tifa every folder below app/ is one URL segment, and a Layout wraps the pages of its folder and below. The words below use "frame" for what a Layout draws around its pages: a header, a sidebar, a footer.
One part of the site has a frame of its own
Name the Layout file of that part layout_.tsx. Its pages show without any Layout above it, the root Layout too.
The tree and the rules are in A Layout without the Layouts above.
Several pages share a frame and can share a URL prefix
Put them in one folder. The folder is the prefix, and its Layout is the frame.
app/
layout.tsx the frame of the public pages
page.tsx /
pricing/
page.tsx /pricing
console/
route.go layout_.tsx the frame of the console and its data
page.tsx /console
settings/
route.go page.tsx /console/settings
Moving between /console and /console/settings keeps the frame mounted, with its state, and does not load its data again. Use layout.tsx in console/ when the console should show inside the root Layout, and layout_.tsx when it should not.
Several pages share a frame and their URLs stay at the same level
Say /dashboard and /settings share the frame of the console, next to / and /pricing, which do not have it, and no prefix is wanted. Give each folder its own Layout that renders the same component with the same data:
app/
layout.tsx the frame of the public pages
page.tsx /
pricing/
page.tsx /pricing
dashboard/
route.go layout_.tsx page.tsx
settings/
route.go layout_.tsx page.tsx
components/
console-frame.tsx the frame, an ordinary component
console/
frame.go package console: type Frame, func LoadFrame
// app/dashboard/route.go, and the same function in app/settings/route.go
package dashboard
func Layout(c *tifa.Ctx) (console.Frame, error) {
return console.LoadFrame(c.Context())
}
// app/dashboard/layout_.tsx, and the same file in app/settings/
import { Outlet } from '@tanstack/react-router'
import { ConsoleFrame } from '@/components/console-frame'
import { layout } from './route.gen'
export default function Layout() {
return (
<ConsoleFrame frame={layout.useLoaderData()}>
<Outlet />
</ConsoleFrame>
)
}
What this costs: /dashboard and /settings are two Layouts that look the same. When the visitor moves from one to the other, the frame is mounted again, so state it holds, such as an open menu or how far a list is scrolled, starts over, and its data is loaded again, in the same request as the data of the page. When that matters, give the pages a prefix as above.
The page at /
app/ cannot hold a layout_.tsx, so the page at / always shows inside the root Layout. Make the root Layout the frame of the page at /, and let the parts with another frame leave it with layout_.tsx, as in the trees above. An app whose pages share no frame at all can leave the root layout.tsx out.
Why Tifa has no route groups
- The path is the URL. Every folder below
app/is one URL segment and one Go package, so you read the URL off the path of a file and find the file from the URL. With route groups a folder may or may not count, and two folders can give the same URL. - The name would be Tifa's own. A folder under
app/is a Go package, and(shop)is not a valid Go import path. Tifa would need a spelling that no other framework has. layout_.tsxalready does the common case. A part of the site that leaves the frame above it needs no group. Route groups would be a second way to do the same thing.
This is settled, and route groups are not planned. A folder named (name) fails tifa check and tifa build with a line that says so.