Next.js Nested Layouts and Route Groups
A layout wraps the pages in its folder. A route group organises folders without changing the address.
The books layout
The layout adds a section heading above the page. The page itself arrives as children.
export default function BooksLayout({ children }: { children: React.ReactNode }) {
return (
<section>
<h2>Books section</h2>
{children}
</section>
);
}The books pages carry the section heading
The check asks for the books page. The layout heading is there.
curl -s http://localhost:3000/books | grep -o "<h2>[^<]*</h2>"The group name is not in the address
The check asks for the group name as an address. It answers 404, while /about answers 200.
curl -s -o /dev/null -w "HTTP %{http_code}\n" "http://localhost:3000/(info)/about"
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3000/aboutExample
console.log("/books → Books section");
console.log("/about → About this app");
console.log("/(info)/about → 404");Key points
- Layout: a file that wraps every page in its folder with a shared frame
- app/books/layout.tsx wraps the books pages. It adds "Books section" above each one.
- A layout stays on screen while the page inside it changes. The frame is shared, not rebuilt.
- Route group: a folder with a name in round brackets, like (info). It groups folders together.
- The group name never appears in the address. The about page in the (info) group opens at /about.
- /(info)/about answers 404. The group name is not a real address.
In this example, three addresses show the two ideas at once. The books pages carry the "Books section" heading from their layout. The about page carries "About this app" from its group layout. The group name itself answers 404, because (info) is for organising, not for addressing.
Note
A group is for organising, not for hiding pages. Every page in the group still opens at its own address.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
What address opens the about page in the (info) group?