Next.jsEasy
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 (
@modalbecomesmodal). 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
asyncand fetch data. - Does not re-render on navigation between its children, so it is cheap.
- Cannot read
searchParamsor the current pathname on the server; useusePathname()oruseSearchParams()in a Client Component child. - Cannot pass data to
childrenvia props. Fetch the same data in the page;fetchmemoization andReact.cachededuplicate it.
Root layout specifics
- Must include
<html>and<body>. - Exports
metadataorgenerateMetadatafor site-wide SEO defaults. - Is the right place for global CSS, fonts from
next/fontand 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.tsx2import type { Metadata } from "next";3import { Geist } from "next/font/google";4import "./globals.css";5import { Providers } from "./providers"; // "use client" context providers67const geist = Geist({ subsets: ["latin"] });89export const metadata: Metadata = {10 title: { default: "Acme", template: "%s | Acme" },11 description: "Acme dashboard",12};1314export 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.