Lesson 1.2

Nested layouts and templates

A layout wraps its segment and every route nested below it.

7mIntermediate17.6k students

Overview

Layouts persist, templates do not

A layout wraps its segment and every route nested below it. On navigation between sibling routes, the layout instance is preserved: its state survives, its effects do not re-run, and only the segment below it swaps out. That is what makes a persistent sidebar or a media player possible.

A template looks identical but remounts on every navigation. Reach for it when you specifically want that reset — an entry animation that should replay, or a form that must clear between records.

Layouts nest, so a route inherits every layout above it. The root layout is special: it owns the html and body tags and is the one layout that cannot be skipped.

If you find yourself fighting a layout to reset state, you probably wanted a template.

In this lesson you will:

  • Share chrome across routes without re-rendering it
  • Choose between a layout and a template
  • Preserve state across navigations inside a layout

Resources

Previous Lesson
Next Lesson
Nested layouts and templates — Next.js for Production — Vertex