What is the layout.js file in the App Router?

layout.js (or .tsx) defines UI shared by a route segment and all its children. It wraps the segment's pages, stays mounted during navigation between them, and preserves state. The root app/layout.tsx is required and defines <html> and <body>.

Props

  • children: the nested layout or page.
  • Slots: one prop per parallel route (@modal becomes modal).
  • params: a Promise of the dynamic params from the root down to this layout.

Use the generated LayoutProps<'/dashboard'> type for all of these.

Behavior

  • Server Component by default; can be async and fetch data.
  • Does not re-render on navigation between its children, so it is cheap.
  • Cannot read searchParams or the current pathname on the server; use usePathname() or useSearchParams() in a Client Component child.
  • Cannot pass data to children via props. Fetch the same data in the page; fetch memoization and React.cache deduplicate it.

Root layout specifics

  • Must include <html> and <body>.
  • Exports metadata or generateMetadata for site-wide SEO defaults.
  • Is the right place for global CSS, fonts from next/font and providers.
  • Do not add <head> tags manually; use the Metadata API.

Performance note

Everything a layout awaits blocks the content below it. With Cache Components, keep request-time reads (cookies, user menus) inside small <Suspense> boundaries, or pass the params promise down instead of awaiting it at the top.

Code example

tsx
1// app/layout.tsx
2import type { Metadata } from "next";
3import { Geist } from "next/font/google";
4import "./globals.css";
5import { Providers } from "./providers"; // "use client" context providers
6
7const geist = Geist({ subsets: ["latin"] });
8
9export const metadata: Metadata = {
10 title: { default: "Acme", template: "%s | Acme" },
11 description: "Acme dashboard",
12};
13
14export default function RootLayout({ children }: LayoutProps<"/">) {
15 return (
16 <html lang="en" className={geist.className}>
17 <body>
18 <Providers>{children}</Providers>
19 </body>
20 </html>
21 );
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app