Most pages on a website share the same frame: a header at the top, a menu at the side, a footer at the bottom. Writing that frame again on every page would be tiring and easy to get wrong. A layout holds the frame once and puts each page inside it. Think of a picture frame: you can change the picture, but the frame stays.
The root layout
Every Next.js app has a root layout in app/layout.tsx. It is required. It wraps every page of the site, and it must contain the html and body tags, because no other file writes them. It is the right place for things that belong to the whole site: the page language, global styles, fonts and the default title.
import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = {
title: "My First Next.js App",
description: "A small site built while learning Next.js.",
};
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
- children is the page, or a deeper layout, that goes inside.
- LayoutProps is a type that Next.js generates for you.
- The lang attribute on html helps screen readers and search engines. Use the language of your content.
- metadata sets the default title and description. We come back to it below.
Nested layouts
A layout file in any route folder wraps that folder and everything below it. The layouts nest, from the outside in: the root layout holds the dashboard layout, and that holds the page. A visitor who moves between /dashboard and /dashboard/settings keeps the same dashboard layout on screen. It does not redraw, and its state is kept.
import Link from "next/link";
export default function DashboardLayout({ children }: LayoutProps<"/dashboard">) {
return (
<div className="flex gap-6">
<aside>
<Link href="/dashboard">Overview</Link>
<br />
<Link href="/dashboard/settings">Settings</Link>
</aside>
<section>{children}</section>
</div>
);
}
We built this and ran it. /dashboard showed the sidebar with the heading Dashboard overview next to it. A different folder that is not under dashboard, such as /about, did not get the sidebar. That is the point of nested layouts: each part of your site can have its own frame.
- A layout can fetch data, because it is a Server Component by default.
- A layout does not receive the search parameters, and it does not redraw when you move between its child pages. That is why it is not the right place for something that depends on the current address.
- Route groups from lesson 7 can hold their own layout, so (marketing) and (account) look different without changing any URLs.
Templates
A template looks like a layout. The difference is what happens when the visitor moves between pages inside it. A layout is kept: its state stays and it is not drawn again. A template is created again on each visit, so its state starts fresh and its effects run again. Use a template when you want that fresh start, for example to run an enter animation on each page or to record each page view.
export default function DashboardTemplate({ children }: { children: React.ReactNode }) {
// A template is created again on every navigation, so state inside it starts fresh.
return <div className="fade-in">{children}</div>;
}
In practice you will use layouts much more than templates. When both exist in a folder, the order is layout first, then template, then page. Reach for a template only when you have a clear reason.
Metadata: the title and description
The title and the description are the lines people read in search results. Next.js has a Metadata API to set them. Export a metadata object from a layout or page, and Next.js writes the matching tags into the head of the page. The values from a deeper file override those of an outer one.
import type { Metadata } from "next";
export const metadata: Metadata = {
title: "About us",
description: "Who we are and what we build.",
};
The title can also be set with a template that adds your site name to every page. Put this in the root layout, and a page that exports the title About us will show About us | My Site.
export const metadata: Metadata = {
title: { default: "My Site", template: "%s | My Site" },
description: "A small site built while learning Next.js.",
};
Dynamic metadata
For a page that depends on the address, export a function called generateMetadata. It receives the same params as the page and returns the metadata. We tested this on a product page.
import type { Metadata } from "next";
const products: Record<string, { name: string; about: string }> = {
"1": { name: "Notebook", about: "A plain paper notebook." },
"2": { name: "Pen", about: "A blue ballpoint pen." },
};
export function generateStaticParams() {
return Object.keys(products).map((id) => ({ id }));
}
export async function generateMetadata({ params }: PageProps<"/products/[id]">): Promise<Metadata> {
const { id } = await params;
const product = products[id];
return {
title: product ? product.name : "Product not found",
description: product?.about,
};
}
export default async function ProductPage({ params }: PageProps<"/products/[id]">) {
const { id } = await params;
const product = products[id];
return <h1>{product ? product.name : "Unknown product"}</h1>;
}
The address /products/1 returned the title Notebook and the description A plain paper notebook. For /products/2 the title was Pen. Search engines and link previews read these tags, so every public page should have its own unique title and description. You will go deeper into metadata in the SEO module.
Your task
- Add a metadata export to your home page and your about page, with different titles and descriptions.
- Create app/dashboard/layout.tsx with a sidebar, and two pages inside it. Move between them and see the sidebar stay.
- Add a template.tsx next to the layout and think about how it differs.
- Create /products/[id] with generateMetadata. View the page source and find the title tag.
How to check it worked
- The sidebar stays while the page changes.
- The page source shows a title and a description tag.
- You can explain the difference between a layout and a template in two sentences.