Part 03 / Topic 3 of 7
layout.tsx vs template.tsx: same position, different lifetime
A layout persists across navigation inside it and never rerenders. A template looks almost identical — same children prop, same default export — but the installed package's own reference for template.js draws the line precisely: "templates are given a unique key, meaning children Client Components reset their state on navigation," useful for "resynchroniz[ing] useEffect on navigation" or "reset[ting] the state of a child Client Component... on navigation." The same reference places it exactly in the tree: "template.js renders between a layout and its children" — layout wraps template, template wraps the page.
Both files can sit in the same folder at once. harness/app/store/(shop)/products/[id]/layout.tsx and harness/app/store/(shop)/products/[id]/template.tsx do exactly that, each adding a marker attribute purely so the nesting order can be read back out of the real rendered HTML instead of taken on faith:
// layout.tsx
export default function ProductLayout({ children }: { children: React.ReactNode }) {
return <div data-wrapper="layout">{children}</div>;
}
// template.tsx
export default function ProductTemplate({ children }: { children: React.ReactNode }) {
return <div data-wrapper="template">{children}</div>;
}
Curled for real, against a running next dev:
$ curl -s http://localhost:PORT/store/products/p1 | grep -o 'data-wrapper="[a-z]*"'
data-wrapper="layout"
data-wrapper="template"
layout appears first in the document, template second and nested inside it — exactly the ordering the docs describe, confirmed against this harness's own markup rather than asserted from the reference alone.
What this build cannot independently re-verify is the remount-on-navigation behavior itself — that a Client Component's state inside template.tsx resets when you navigate from /store/products/p1 to /store/products/p2, while the same state inside layout.tsx would have survived. Proving that requires watching a client runtime's state across a real browser navigation, which is outside what curl and next build can observe; this course's harness has no browser-automation tool installed to do it safely. That specific claim is therefore stated directly from the installed package's own template.js reference (quoted above), not independently re-run — everything else in this section (the file structure, the build staying green, and the nesting order in the rendered HTML) is independently verified against this harness.
In practice: reach for template.tsx only when a route segment needs that reset — a search box that should clear on every visit to a results page, a per-visit animation, an effect that must resynchronize. Apna Bazaar's product page doesn't actually need this (nothing in it holds client state yet), so this section exists to show the mechanism, not to recommend keeping template.tsx in the harness going forward.
Full files: examples/05-product-layout.tsx, examples/06-product-template.tsx.
Done here? The practice set for Part 3 — including the solutions — is on the part page.