Lesson 3 of 9 · 22m
Routing, Layouts, and Navigation State
Nested layouts preserve state and eliminate repeated work, but only when you understand what rerenders, what persists, and when navigation is really a data transition.
Nested routing is the feature teams use every day while rarely naming correctly. A route segment is not just a URL piece; it is a persistent branch in a UI tree. Layouts stay mounted across sibling navigations, pages swap inside them, and templates intentionally remount. Once you model it as a tree, odd bugs — losing scroll, resetting filters, repeated auth fetches — become predictable instead of spooky.
Layouts preserve; templates remount
app/
console/
layout.tsx # persists across /console/* navigation
page.tsx
projects/
layout.tsx # persists across project subroutes
page.tsx
settings/page.tsxUse layout.tsx for shells that should keep state: navigation expansion, selected workspace, media players, tabs implemented as client state. Use template.tsx only when you intentionally want a fresh instance on every navigation — enter animations, per-view analytics mounts, or resetting local state.
Server navigation is a request, not a DOM patch
When the user clicks a <Link>, Next.js asks the server for the next route's React Server Component payload. That means navigation can trigger data fetching, caching decisions, streaming, redirects, and auth checks. It is not merely swapping JSON in the browser.
<Link href="/console/projects/42" prefetch={false}>
Project 42
</Link>Automatic prefetching is useful for static or cheap routes. For expensive personalized routes, disable prefetch or use a plain link in dense lists. I have watched a billing table prefetch two hundred customer detail pages because every row was a link; the user never hovered and the database paid for it.
URL state belongs in the URL
Tabs, filters, pagination, and search should usually live in search params:
export default async function UsersPage({ searchParams }: { searchParams: Promise<{ q?: string; page?: string }> }) {
const { q = "", page = "1" } = await searchParams;
const users = await searchUsers(q, Number(page));
return <UserResults users={users} />;
}In Next.js 15+, searchParams and params are asynchronous request data. Awaiting them is not optional ceremony; it is part of the type contract. Client-side edits use useRouter().replace() or router.push() with a new query string.
Parallel routes are not a free layout engine
@modal, @sidebar, and friends are powerful when two independent branches share a screen. They also multiply not-found, error, and loading behavior. Use them for genuinely parallel UI — an overlay modal with a background page — not because folders look tidy.
Field notes
usePathname,useSearchParams, anduseRouterare client hooks. Reading them pulls that component into the client graph.router.refresh()asks the server to rerender the current route while preserving client state. It is not a cache purge by itself.- Route handlers under
app/api/.../route.tsare endpoints, not page data plumbing. Do not proxy every page fetch through them out of habit. - The loading skeleton that appears instantly can make an app feel slower if it flashes for 80 milliseconds. Use streaming thoughtfully, not everywhere.
Was this lesson helpful?