template.tsx wraps pages inside the layout chain - special file, no URL, nearest-wins.
A template.tsx file wraps each page inside the layout chain. Like layout.tsx, it is a special file and does not create a URL. Unlike a layout, it is resolved per page scope and is a good place for effects that should run when navigating between pages.
| File | Role |
|---|---|
| layout.tsx | Wraps segment + children, receives params |
| template.tsx | Wraps page inside layout chain, receives children |
| page.tsx | Route content |
children only - not params.Both are special files that wrap page content without creating a URL. They differ in what they receive and in how they behave on navigation.
| Feature | layout.tsx | template.tsx |
|---|---|---|
| Wraps | Segment + all children | Page inside layout chain |
| Props | params + children (Outlet) | children only |
| Typical use | Nav, sidebar, shared chrome | Page-level effects, transitions |
| State persistence | Persists across navigation | Resets on every navigation |
In practice, put persistent chrome - navigation bars, sidebars, footers - in a layout.tsx. Put anything that should re-run or reset when the route changes in a template.tsx.
Create template.tsx in any folder. It must have a default export and receives children.
Because templates are unmounted and remounted on navigation, they are useful for running effects once per page view:
html element are ignored, the same way layouts are. Prefer simple wrappers around children.Prefer a template when you need page-scoped behavior without changing the persistent layout chrome.
| Use Case | Why template? |
|---|---|
| Page transitions | Wrap the page for enter/exit animation classes |
| Analytics / logging | Run effects on every page view |
| Focus management | Move focus when the page content changes |
| Reset local UI state | Keep layout state, but re-initialize page-level UI on navigation |
Templates next to layouts, loading, and error. Special files do not create URLs.
| File | Purpose |
|---|---|
| src/app/layout.tsx | Root layout |
| src/app/template.tsx | Root template |
| src/app/page.tsx | Home page |
| src/app/dashboard/template.tsx | Dashboard page wrapper |
| src/app/dashboard/page.tsx | Dashboard page |
layout.tsx for shared chrome. Use template.tsx for page-level wrapping inside that chrome.Catch render errors and show a fallback UI.
Fallback for a parallel route slot with no match.
Show a fallback while pages and layouts load.
Create your first pages and wrap them in shared layouts.
Define routes with folders and page files inside them.
How every route is pre-rendered to HTML at build time.
template.tsx wraps pages inside the layout chain - special file, no URL, nearest-wins.
A template.tsx file wraps each page inside the layout chain. Like layout.tsx, it is a special file and does not create a URL. Unlike a layout, it is resolved per page scope and is a good place for effects that should run when navigating between pages.
| File | Role |
|---|---|
| layout.tsx | Wraps segment + children, receives params |
| template.tsx | Wraps page inside layout chain, receives children |
| page.tsx | Route content |
children only - not params.Both are special files that wrap page content without creating a URL. They differ in what they receive and in how they behave on navigation.
| Feature | layout.tsx | template.tsx |
|---|---|---|
| Wraps | Segment + all children | Page inside layout chain |
| Props | params + children (Outlet) | children only |
| Typical use | Nav, sidebar, shared chrome | Page-level effects, transitions |
| State persistence | Persists across navigation | Resets on every navigation |
In practice, put persistent chrome - navigation bars, sidebars, footers - in a layout.tsx. Put anything that should re-run or reset when the route changes in a template.tsx.
Create template.tsx in any folder. It must have a default export and receives children.
Because templates are unmounted and remounted on navigation, they are useful for running effects once per page view:
html element are ignored, the same way layouts are. Prefer simple wrappers around children.Prefer a template when you need page-scoped behavior without changing the persistent layout chrome.
| Use Case | Why template? |
|---|---|
| Page transitions | Wrap the page for enter/exit animation classes |
| Analytics / logging | Run effects on every page view |
| Focus management | Move focus when the page content changes |
| Reset local UI state | Keep layout state, but re-initialize page-level UI on navigation |
Templates next to layouts, loading, and error. Special files do not create URLs.
| File | Purpose |
|---|---|
| src/app/layout.tsx | Root layout |
| src/app/template.tsx | Root template |
| src/app/page.tsx | Home page |
| src/app/dashboard/template.tsx | Dashboard page wrapper |
| src/app/dashboard/page.tsx | Dashboard page |
layout.tsx for shared chrome. Use template.tsx for page-level wrapping inside that chrome.Catch render errors and show a fallback UI.
Fallback for a parallel route slot with no match.
Show a fallback while pages and layouts load.
Create your first pages and wrap them in shared layouts.
Define routes with folders and page files inside them.
How every route is pre-rendered to HTML at build time.