Skip to content

Layouts

A layout is a component that wraps pages. Headers, navigation, and footers belong in layouts, so each page contains only its own content. Kiln applies layouts by folder: a _layout file wraps every page in its folder and below.

Writing a layout

A layout is a file named _layout.tsx (or .jsx, .ts, .js). It exports a default component that receives its content as children:

tsx
// app/pages/_layout.tsx
import type { ReactNode } from "react";
import { Link } from "@kiln-js/client";

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <>
      <nav>
        <Link href="/">Home</Link> <Link href="/about">About</Link>
      </nav>
      <main>{children}</main>
    </>
  );
}

Every page in the app now renders inside <main>, beneath the navigation.

Nested layouts

Layouts stack. Each folder can have its own _layout, and every layout from the root down to the page's folder applies, with the outermost first:

text
app/pages/_layout.tsx                  RootLayout
app/pages/docs/_layout.tsx             DocsLayout
app/pages/docs/guide.tsx               Page

The rendered tree is <RootLayout><DocsLayout><Page /></DocsLayout></RootLayout>. The scanner finds the layouts for each route in advance, so no layout is searched for at request time.

Layouts and route groups

Route groups affect layouts but not URLs. Use a group to give a set of pages a layout that other pages do not get:

text
app/pages/_layout.tsx                    applies to everything
app/pages/(marketing)/_layout.tsx        applies to marketing pages only
app/pages/(marketing)/pricing.tsx        /pricing, gets both layouts
app/pages/app/dashboard.tsx              /app/dashboard, gets only the root layout

Layouts and navigation

Layouts stay mounted during client-side navigation when a folder's layouts do not change. Their state survives, so a search box or a sidebar keeps its contents as you move between pages. Kiln places layouts outside the page's error boundary, so a crash inside a page does not take the navigation down with it.

Layouts and loaders

A layout does not have its own loader in the same way a page does. The data a layout needs is either read in the component through hooks, or fetched by each page. Loader data belongs to the page's data prop. Layouts receive only children.

Layouts receive only children as a prop, but they sit inside the route context, so useParams() and usePathname() work in them. Use those hooks to read the current route:

tsx
import { usePathname } from "@kiln-js/core";

export default function DocsLayout({ children }: { children: ReactNode }) {
  const pathname = usePathname();
  return (
    <div className="docs">
      <aside>Current: {pathname}</aside>
      {children}
    </div>
  );
}

Modes and layouts

A .server page uses its layouts on the server, and the layouts ship no JavaScript on their own. If a layout contains interactive parts, use islands for them, or move the layout to a universal folder. See Rendering modes.

Generating a layout

bash
bunx kiln generate layout (marketing)

The command creates app/pages/(marketing)/_layout.tsx.

Things to avoid

  • Do not put a page's data fetching in a layout. Pages own their loaders.
  • Do not rely on a layout's effects running again on every navigation. They run when the layout mounts, and again only when it is remounted.
  • Do not use <html> or <body> in a layout. Kiln's document template provides them. Put global styles and scripts in the template or in the client entry.