Skip to content

Chapter 5 of 8

Layouts, Templates, and Special Files

Layouts are one of the App Router's most powerful features. A layout.tsx file in any folder defines the chrome that wraps every page beneath that segment, such as a navigation bar, footer, or sidebar. Because layouts persist across navigations between their child routes, React state inside a layout survives route changes, providing a snappy and uninterrupted experience. The root layout located at app/layout.tsx is mandatory and wraps the entire application; it is also where you typically set up the <html> and <body> tags.

Templates look similar to layouts but behave very differently. A template.tsx file creates a new instance on every navigation, which means its effects re-run, its local state resets, and the entire component remounts. This is useful when you want each route to start with fresh state, for example an animation that should replay on each page transition or a form that should clear when the user navigates away and back. Choosing between a layout and a template therefore comes down to whether you want state continuity or fresh state.

Beyond layouts and templates, Next.js recognizes a handful of other special files that improve the user experience. Adding a loading.tsx to a route segment automatically wraps the page in a React Suspense boundary, so Next.js can stream the loading UI while the actual content is still being fetched. An error.tsx file, which must be a Client Component, acts as an error boundary that catches errors in its segment and its children, exposing both the error and a reset function so the user can retry. Finally, not-found.tsx provides a custom 404 page that renders when notFound() is called from a Server Component or when no route matches the requested URL. Streaming ties all of this together: the server can send UI in chunks as it becomes ready, rather than waiting for all data to be available, and Suspense boundaries mark where streamed content can be revealed progressively, improving both Time to First Byte and First Contentful Paint.

All chapters
  1. 1Foundations of Next.js and the App Router
  2. 2Server and Client Components
  3. 3Rendering Strategies
  4. 4Advanced Routing Patterns
  5. 5Layouts, Templates, and Special Files
  6. 6Data Fetching, Mutations, and Middleware
  7. 7Navigation Hooks and Components
  8. 8Configuration, Metadata, and Optimization

Drill it

Reading is not remembering. These come from the Nextjs Framework deck:

Q

What is Next.js?

Next.js is a React framework created by Vercel that provides features like server-side rendering, static site generation, file-based routing, and API routes out...

Q

What is the App Router in Next.js?

The App Router (introduced in Next.js 13) uses the app/ directory and supports React Server Components, nested layouts, streaming, and colocation of files. It r...

Q

How does file-based routing work in the App Router?

Routes are defined by the folder structure inside app/. A route is created by adding a page.tsx file inside a folder. For example, app/about/page.tsx maps to th...

Q

What is a Server Component in Next.js?

A Server Component renders on the server and sends HTML to the client. It is the default in the App Router. Server Components can directly access databases, rea...