Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 03 / Stage 1

Layouts, templates and navigation

Goal: Wrap Apna Bazaar's routes in a shared, persistent layout; learn exactly where template.tsx differs from layout.tsx; reach for route groups a second time, now to scope a layout rather than just to organize files; navigate with next/link, build an active-link nav with usePathname, and know precisely when to reach for useRouter instead of redirect(). Time: ~5 h · Level: 🟢 finished Part 2, comfortable with dynamic segments, route groups and notFound()

What you will learn

  • Nested layouts: one layout.tsx wrapping every route under /store, and why it doesn't rerender on navigation
  • Route groups used for a second reason beyond Part 2's file organization: scoping a layout to only part of a route tree
  • layout.tsx vs template.tsx — same position in the tree, different lifetime, proven against the real rendered HTML
  • <Link>'s prefetching and client-side transitions, read straight from the installed package's own guide
  • The active-link pattern: usePathname() in a Client Component, compared against the current URL
  • useRouter() vs redirect() — two different tools for "send the user somewhere else," used in two different kinds of code
  • Reading PageProps/generated route types again, now including a route built in this part

Topics in this part

Practice

Every exercise adds or edits files under harness/app/store/, runs a real command against them, and then restores the harness to the exact state Part 3's README ends on — the same state Part 4 starts from. Copy the exact commands you ran and their real output into your own notes; do not type output from memory.

Two of these exercises (4 and 7) are "break it on purpose" exercises, the same pattern Part 2's exercises 5 and 6 used — reproducing a real error with your own hands is worth more than reading about it.


Exercise 1: A layout scoped to just the products section

Scenario: Apna Bazaar wants a small "📦 Full catalog section" note on both /store/products and every /store/products/[id] page, but nowhere else. What to do: Create app/store/(shop)/products/layout.tsx wrapping children in a <div data-section="products"> with that note above them. Rebuild, then confirm with curl that the marker appears on /store/products and /store/products/p1, but not on /store or /store/about.

Sample output (run for real against this course's harness):

$ curl -s http://localhost:PORT/store/products | grep -o 'data-section="[a-z]*"'
data-section="products"
$ curl -s http://localhost:PORT/store/products/p1 | grep -o 'data-section="[a-z]*"'
data-section="products"
$ curl -s http://localhost:PORT/store | grep -o 'data-section="[a-z]*"'
(no output)

Delete app/store/(shop)/products/layout.tsx afterward — it is not part of this part's canonical harness state. 💡 Hint: This is the README's section 2 idea, one level deeper: a layout placed inside (shop)/products/ wraps only what's inside that folder, same as (shop)/layout.tsx wraps only what's inside (shop)/.


Exercise 2: A new route group, scoped from scratch

Scenario: Apna Bazaar wants a Diwali offers page, /store/offers, with its own "⚡ Limited-time offer" banner that nothing else on the site gets. What to do: Create a third group, app/store/(deals)/offers/page.tsx (an <h1>Diwali Offers</h1> is enough) and app/store/(deals)/layout.tsx with the banner. Rebuild and confirm /store/offers appears in the route table as ○ (Static), and that the banner shows there but nowhere else.

Sample output (run for real against this course's harness):

$ npm run build
...
Route (app)
┌ ○ /
├ ○ /_not-found
├ ○ /store
├ ○ /store/about
├ ○ /store/contact
├   /store/go/[id]
│ ├ ◐ /store/go/[id]
│ ├ ○ /store/go/p1
│ ├ ○ /store/go/p2
│ └ ○ /store/go/p3
├ ○ /store/offers
├ ○ /store/products
├   /store/products/[id]
│ ├ ◐ /store/products/[id]
│ ├ ○ /store/products/p1
│ ├ ○ /store/products/p2
│ └ ○ /store/products/p3
└   /store/search/[...terms]
  ├ ◐ /store/search/[...terms]
  ├ ○ /store/search/silk
  └ ○ /store/search/pottery

$ curl -s http://localhost:PORT/store/offers | grep -o 'data-scope="[a-z]*"'
data-scope="deals"
$ curl -s http://localhost:PORT/store | grep -o 'data-scope="[a-z]*"'
data-scope="shop"

Delete app/store/(deals)/ afterward. 💡 Hint: A brand-new group works exactly like (shop) and (info) already do — nothing about creating a third group is different from creating the first two.


Exercise 3: A fifth nav link, and the nested-match rule

Scenario: StoreNav only has four links. Add a fifth, "Search", pointing at /store/search, and confirm the active-link logic still gets it right on a page two levels deeper. What to do: Add { href: "/store/search", label: "Search" } to the links array in app/store/_components/StoreNav.tsx. Restart next dev (a page that was already statically generated can otherwise keep serving its old markup — see the Common Mistakes table), then curl /store/search/silk and /store.

Sample output (run for real against this course's harness):

$ curl -s http://localhost:PORT/store/search/silk | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a style="font-weight:400;text-decoration:none" href="/store">Home</a><a style="font-weight:400;text-decoration:none" href="/store/products">Products</a><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store/search">Search</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>
$ curl -s http://localhost:PORT/store | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store">Home</a><a style="font-weight:400;text-decoration:none" href="/store/products">Products</a><a style="font-weight:400;text-decoration:none" href="/store/search">Search</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>

Remove the added line from links afterward, and restart next dev once more. 💡 Hint: /store/search/silk is two segments past /store/search, and the nav still gets it right, because pathname.startsWith("/store/search") doesn't care how many segments come after it.


Exercise 4: Layout wraps template, proven on a second route

Scenario: The README's section 3 proved layout.tsx wraps template.tsx only on /store/products/[id]. Prove the same ordering holds on a completely different segment. What to do: Add app/store/(shop)/search/[...terms]/layout.tsx and a sibling template.tsx, each wrapping children with a distinct data-wrapper value (e.g. search-layout and search-template). Curl /store/search/silk and read the order back.

Sample output (run for real against this course's harness):

$ curl -s http://localhost:PORT/store/search/silk | grep -o 'data-wrapper="[a-z-]*"'
data-wrapper="search-layout"
data-wrapper="search-template"

Delete both files afterward. 💡 Hint: The order never depends on which route segment you're in — layout always comes first in the markup, template always second, for the same reason section 3 gives: template sits structurally between a layout and the page it wraps.


Exercise 5: redirect() vs permanentRedirect()

Scenario: Apna Bazaar decides the /store/go/:id mapping will never change, so the temporary (307) redirect should become permanent (308). What to do: In app/store/(shop)/go/[id]/page.tsx, swap the import and call from redirect to permanentRedirect (both from next/navigation). Curl the route and read the status code.

Sample output (run for real against this course's harness):

$ curl -s -D - -o /dev/null http://localhost:PORT/store/go/p1 | grep -iE "^(HTTP|location)"
HTTP/1.1 308 Permanent Redirect
location: /store/products/p1

Change the import and call back to redirect afterward — this harness's canonical /store/go/[id] is a 307, matching the README. 💡 Hint: Nothing else in the file needs to change. redirect and permanentRedirect share the same signature; only the status code they produce (and whether a browser is allowed to cache the redirect itself) differs.


Exercise 6: Break the exact-match exception on purpose

Scenario: Section 5 of the README calls out an exact-match exception for the /store link, without which "Home" would read as active on every single storefront page. Prove that by removing it. What to do: In app/store/_components/StoreNav.tsx, replace the conditional —

const isActive = link.href === "/store" ? pathname === "/store" : pathname.startsWith(link.href);

— with the simpler, buggy version:

const isActive = pathname.startsWith(link.href);

Restart next dev and curl /store/products.

Sample output (run for real against this course's harness):

$ curl -s http://localhost:PORT/store/products | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store">Home</a><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store/products">Products</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>

Both "Home" and "Products" carry aria-current="page" at once — a real bug, not a hypothetical one. Restore the exact-match version afterward. 💡 Hint: Every href in links starts with /store, including /store itself. startsWith alone can never distinguish "the pathname is exactly this link's href" from "the pathname is this link's href plus more" for the one link whose href is a prefix of all the others.


Exercise 7: Remove generateStaticParams and watch two errors, not one

Scenario: Part 2 section 4 showed that a dynamic route without generateStaticParams fails next build under cacheComponents: true. Reproduce it here, on /store/go/[id], and notice it's not the only thing that breaks. What to do: Temporarily delete the generateStaticParams export (and its now-unused products import) from app/store/(shop)/go/[id]/page.tsx, leaving just the redirect() call. Run next build.

Sample output (run for real against this course's harness):

$ npm run build
...
Error: Route "/store/go/[id]": Next.js encountered URL data `usePathname()` in a Client Component outside of `<Suspense>`.

This blocks prerendering because the value is only available at runtime.
...
    at <unknown> (app/store/_components/StoreNav.tsx:18:20)
...
  digest: 'CLIENT_HOOK_DYNAMIC'
}
Error: Route "/store/go/[id]": Next.js encountered uncached or runtime data during prerendering.

`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.
...
Export encountered an error on /store/(shop)/go/[id]/page: /store/go/[id], exiting the build.

Restore generateStaticParams (and the products import) afterward, and confirm next build is green again. 💡 Hint: Two separate errors fire, not one. The second is the familiar one from Part 2 — the page itself can't resolve params for an unknown id. The first is new: StoreNav's usePathname() call, sitting in the shared layout that wraps every route under /store, also can't resolve "what page am I on" for a param Next.js doesn't know ahead of time — the same "fallback param" caveat the installed package's own usePathname/useSelectedLayoutSegment reference describes. Every other dynamic route in this harness already has generateStaticParams, which is exactly why only this one, briefly missing it, surfaces the problem.

Show the solutions for Part 3 (7 files)

solutions/ex01.tsx

// Solution to Exercise 1. Drop in as
// harness/app/store/(shop)/products/layout.tsx, then rebuild and curl
// /store/products, /store/products/p1 and /store for the data-section
// attribute. Delete the file afterward — it is not part of the harness's
// canonical Part 3 state.
export default function ProductsSectionLayout({ children }: { children: React.ReactNode }) {
  return (
    <div data-section="products">
      <p style={{ fontFamily: "system-ui", padding: "0 2rem", color: "#888", fontSize: "0.8rem" }}>
        📦 Full catalog section
      </p>
      {children}
    </div>
  );
}

solutions/ex02.md

# Solution to Exercise 2: a new route group, scoped from scratch

Two new files, a group and a page inside it:

```tsx
// harness/app/store/(deals)/offers/page.tsx
export default function OffersPage() {
  return (
    <main>
      <h1>Diwali Offers</h1>
      <p>Flat 10% off on all pottery this week.</p>
    </main>
  );
}
```

```tsx
// harness/app/store/(deals)/layout.tsx
export default function DealsLayout({ children }: { children: React.ReactNode }) {
  return (
    <div data-scope="deals">
      <p style={{ fontFamily: "system-ui", padding: "0 2rem", color: "#a33", fontSize: "0.85rem" }}>
        ⚡ Limited-time offer
      </p>
      {children}
    </div>
  );
}
```

Rebuild and read the route table — `/store/offers` shows up `○` (Static), same as every other storefront page, and nothing else in the table moved:
```text
$ npm run build
...
Route (app)
┌ ○ /
├ ○ /_not-found
├ ○ /store
├ ○ /store/about
├ ○ /store/contact
├   /store/go/[id]
│ ├ ◐ /store/go/[id]
│ ├ ○ /store/go/p1
│ ├ ○ /store/go/p2
│ └ ○ /store/go/p3
├ ○ /store/offers
├ ○ /store/products
├   /store/products/[id]
│ ├ ◐ /store/products/[id]
│ ├ ○ /store/products/p1
│ ├ ○ /store/products/p2
│ └ ○ /store/products/p3
└   /store/search/[...terms]
  ├ ◐ /store/search/[...terms]
  ├ ○ /store/search/silk
  └ ○ /store/search/pottery
```

Then confirm the banner is scoped correctly — present on `/store/offers`, a different banner on `/store` (from `(shop)`), neither crossing over:
```text
$ curl -s http://localhost:PORT/store/offers | grep -o 'data-scope="[a-z]*"'
data-scope="deals"
$ curl -s http://localhost:PORT/store | grep -o 'data-scope="[a-z]*"'
data-scope="shop"
```

Delete `harness/app/store/(deals)/` afterward — three groups, `(shop)`, `(info)` and `(deals)`, would all still work side by side if you kept it, but it isn't part of this part's canonical harness state.

solutions/ex03.md

# Solution to Exercise 3: a fifth nav link, and the nested-match rule

In `harness/app/store/_components/StoreNav.tsx`, add one entry to `links`:

```ts
const links = [
  { href: "/store", label: "Home" },
  { href: "/store/products", label: "Products" },
  { href: "/store/search", label: "Search" }, // added
  { href: "/store/about", label: "About" },
  { href: "/store/contact", label: "Contact" },
];
```

Nothing else in the file changes — the `isActive` logic already handles any link whose `href` isn't `/store` itself with `startsWith`, so the new "Search" link needs no special case.

Restart `next dev` before testing (a route that was already statically generated under the old component code can otherwise keep serving stale markup from an earlier request — the Common Mistakes table calls this out). Then:
```text
$ curl -s http://localhost:PORT/store/search/silk | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a style="font-weight:400;text-decoration:none" href="/store">Home</a><a style="font-weight:400;text-decoration:none" href="/store/products">Products</a><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store/search">Search</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>
$ curl -s http://localhost:PORT/store | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store">Home</a><a style="font-weight:400;text-decoration:none" href="/store/products">Products</a><a style="font-weight:400;text-decoration:none" href="/store/search">Search</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>
```
`/store/search/silk` is two path segments past `/store/search`, and "Search" still reads active — `pathname.startsWith("/store/search")` is `true` for `/store/search/silk` regardless of what follows.

Remove the added line afterward and restart `next dev` once more to get back to the canonical four-link nav.

solutions/ex04.md

# Solution to Exercise 4: layout wraps template, proven on a second route

Two new sibling files:

```tsx
// harness/app/store/(shop)/search/[...terms]/layout.tsx
export default function SearchLayout({ children }: { children: React.ReactNode }) {
  return <div data-wrapper="search-layout">{children}</div>;
}
```

```tsx
// harness/app/store/(shop)/search/[...terms]/template.tsx
export default function SearchTemplate({ children }: { children: React.ReactNode }) {
  return <div data-wrapper="search-template">{children}</div>;
}
```

Curl any search result and read the marker order:
```text
$ curl -s http://localhost:PORT/store/search/silk | grep -o 'data-wrapper="[a-z-]*"'
data-wrapper="search-layout"
data-wrapper="search-template"
```
`search-layout` comes first in the markup, `search-template` second and nested inside it — the same order the README's section 3 found on `/store/products/[id]`, now confirmed on a completely different route segment (a catch-all, not a single dynamic id). The ordering doesn't depend on which kind of dynamic segment it is; it depends only on `layout.js` always sitting above `template.js` in the component hierarchy, per the installed package's own reference.

Delete both files afterward — they are not part of this part's canonical harness state.

solutions/ex05.md

# Solution to Exercise 5: `redirect()` vs `permanentRedirect()`

In `harness/app/store/(shop)/go/[id]/page.tsx`, swap one import and one call:

```diff
-import { redirect } from "next/navigation";
+import { permanentRedirect } from "next/navigation";
 import { products } from "../../../lib/products";

 export function generateStaticParams() {
   return products.map((product) => ({ id: product.id }));
 }

 export default async function GoPage({ params }: PageProps<"/store/go/[id]">) {
   const { id } = await params;
-  redirect(`/store/products/${id}`);
+  permanentRedirect(`/store/products/${id}`);
 }
```

Nothing else changes — same signature, same `generateStaticParams`, same import source (`next/navigation`). Curled for real:
```text
$ curl -s -D - -o /dev/null http://localhost:PORT/store/go/p1 | grep -iE "^(HTTP|location)"
HTTP/1.1 308 Permanent Redirect
location: /store/products/p1
```
308 instead of 307 — exactly the distinction the README's section 6 table draws between the two functions: `redirect` defaults to a 307 (temporary) outside a Server Action, `permanentRedirect` always sends a 308.

Revert the import and call back to `redirect` afterward — this harness's canonical `/store/go/[id]` is a temporary redirect, matching the README.

solutions/ex06.md

# Solution to Exercise 6: break the exact-match exception on purpose

In `harness/app/store/_components/StoreNav.tsx`, simplify the conditional until it loses the exact-match exception:

```diff
-        const isActive = link.href === "/store" ? pathname === "/store" : pathname.startsWith(link.href);
+        const isActive = pathname.startsWith(link.href);
```

Restart `next dev` and curl a page one level below `/store`:
```text
$ curl -s http://localhost:PORT/store/products | grep -o '<nav[^>]*>.*</nav>'
<nav style="display:flex;gap:1rem;padding:1rem 2rem 0;font-family:system-ui"><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store">Home</a><a aria-current="page" style="font-weight:700;text-decoration:underline" href="/store/products">Products</a><a style="font-weight:400;text-decoration:none" href="/store/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>
```
Real bug, reproduced: both "Home" and "Products" carry `aria-current="page"` on the same page, because `/store/products`'s pathname genuinely does start with `/store`, and `/store` is also itself a valid prefix of its own `href`. Every link's `href` in this nav starts with `/store` — `startsWith` alone can never tell "this is exactly that link's target" apart from "this is that link's target plus more," for the one link whose target is a prefix of everyone else's.

Restore the exact-match version afterward:
```ts
const isActive = link.href === "/store" ? pathname === "/store" : pathname.startsWith(link.href);
```

solutions/ex07.md

# Solution to Exercise 7: remove `generateStaticParams`, watch two errors

Temporarily reduce `harness/app/store/(shop)/go/[id]/page.tsx` to just the redirect itself:

```tsx
import { redirect } from "next/navigation";

export default async function GoPage({ params }: PageProps<"/store/go/[id]">) {
  const { id } = await params;
  redirect(`/store/products/${id}`);
}
```
(dropping the `generateStaticParams` export and the now-unused `products` import).

Run the build:
```text
$ npm run build
...
Error: Route "/store/go/[id]": Next.js encountered URL data `usePathname()` in a Client Component outside of `<Suspense>`.

This blocks prerendering because the value is only available at runtime.

Ways to fix this:
  - [stream] Wrap the component in `<Suspense fallback={...}>` so the hook value streams in after prerendering
  - [block] Set `export const instant = false` to allow a blocking route

Learn more: https://nextjs.org/docs/messages/blocking-prerender-client-hook
    at <unknown> (app/store/_components/StoreNav.tsx:18:20)
    at body (<anonymous>)
    at html (<anonymous>)
  16 |
  17 | export function StoreNav() {
> 18 |   const pathname = usePathname();
     |                    ^
  digest: 'CLIENT_HOOK_DYNAMIC'
}
Error: Route "/store/go/[id]": Next.js encountered uncached or runtime data during prerendering.

`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.

Ways to fix this:
  - [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access
  - [cache] For uncached data (`fetch`, database calls): cache the access with `"use cache"` (does not apply to `connection()`)
  - [block] Set `export const instant = false` to allow a blocking route

Learn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic
Error occurred prerendering page "/store/go/[id]". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /store/(shop)/go/[id]/page: /store/go/[id], exiting the build.
```

Two errors, not one, and the first one is the interesting part. With no `generateStaticParams`, `/store/go/[id]`'s `id` becomes a "fallback param" — unknown until an actual request arrives. `StoreNav`'s `usePathname()` call sits in `app/store/layout.tsx`, which wraps **every** route under `/store`, including this one, and the installed package's own reference for this family of hooks says a fallback param forces exactly this: the hook "cannot be resolved during prerendering," so it needs a `<Suspense>` boundary around it — or, as here, it blocks the build outright because the harness has none. The second error is the one Part 2 section 4 already covered: the page's own `params` read has the same problem, independently.

Every other dynamic route already in this harness (`/store/products/[id]`, `/store/go/[id]` in its real, correct form, `/store/search/[...terms]`) has `generateStaticParams`, which is exactly why this is the only place the `usePathname()` error ever surfaces — remove the one dynamic route's static-params list, and the shared layout's nav breaks too, not just that one page.

Restore the full file:
```tsx
import { redirect } from "next/navigation";
import { products } from "../../../lib/products";

export function generateStaticParams() {
  return products.map((product) => ({ id: product.id }));
}

export default async function GoPage({ params }: PageProps<"/store/go/[id]">) {
  const { id } = await params;
  redirect(`/store/products/${id}`);
}
```
and confirm `npm run build` is green again.

Common mistakes

Mistake Why it is wrong Fix
Expecting layout.tsx to rerun on every navigation The installed package's docs state layouts "do not rerender" on navigation within them — reproduced structurally in section 1, by component hierarchy in section 3 Put per-navigation reset logic in template.tsx instead (section 3)
Reaching for template.tsx by default It is the exception, not the default — most routes want a layout's persistence, not a template's reset Default to layout.tsx; add template.tsx only when a specific remount-on-navigation need exists
Forgetting the exact-match exception on a root nav link /store's href is a prefix of every other link's href; a naive startsWith check marks "Home" active everywhere, reproduced if you remove the exception in section 5 Use an exact match for the link whose href is a prefix of the others, startsWith for the rest
Calling redirect() inside a Client Component event handler The installed package's docs are explicit: redirect works in Client Components "during the rendering process but not in event handlers" Use useRouter().push inside the handler instead (section 6)
Assuming redirect() returns like a normal function It throws (NEXT_REDIRECT, proven in section 6), the same throw-based pattern as notFound()'s NEXT_HTTP_ERROR_FALLBACK;404 from Part 2 Never wrap it in a try/catch that would swallow the throw; call it outside any try block
Expecting a route group to show up in AppRoutes or the URL Proven twice now — Part 2's (info) and this part's (shop) — groups are invisible to both Use groups freely for organization or layout scoping; never rely on their name appearing anywhere public

Quick recap

  • A nested layout.tsx wraps every route below it and persists across navigation inside it — Part 1's root layout wrapped exactly one route; this part's app/store/layout.tsx wraps all of /store.
  • Route groups do double duty: Part 2 used (info) purely to organize files; this part used (shop) and (info) to scope two different layouts to two different halves of the same route tree, with zero effect on any URL.
  • layout.tsx and template.tsx can coexist in one folder; layout wraps template wraps the page, proven against real rendered HTML, while the remount-on-navigation difference itself is sourced directly from the installed package's reference.
  • <Link> prefetches and performs a client-side transition instead of a full document reload — the mechanism every layout's "doesn't rerender" claim depends on.
  • usePathname() in a Client Component reads the current URL to build an active-link nav; watch for the exact-match exception on whichever link's href is a prefix of the others.
  • redirect() (Server Components/Functions/Route Handlers, throws, 307 by default) and useRouter().push (Client Component event handlers, imperative) solve the same problem in two places that never overlap.

Checklist

  • I built a nested layout and watched it wrap every route under it without changing the route table.
  • I moved existing pages into a route group purely to scope a layout to them, and confirmed their URLs didn't change.
  • I put layout.tsx and template.tsx in the same folder and read their real nesting order out of curled HTML.
  • I can state, unprompted, why <Link> beats a plain <a> tag for navigation inside this app.
  • I built an active-link nav with usePathname and got the exact-match exception right on the first try — or found the bug by testing it.
  • I called redirect() from a Server Component and read its 307 status and NEXT_REDIRECT digest for real.
  • I called useRouter().push from a button's onClick and can explain why redirect() could not have gone there instead.

Search the guide