A <Link> navigation doesn't reload the document. Next.js compares the old and new routes, keeps the layouts they share, and renders only the segments below the deepest shared layout. Because the layout's elements stay at the same position with the same component types, React keeps their state: that's ordinary reconciliation, not a Next.js cache. A sidebar's open menu, a search box or a playing video in a layout all survive. The layout also doesn't re-render on the server, which is why it gets no searchParams or pathname.
template.js wraps the same children but receives a key for its own segment. When that segment's value changes (/blog/a → /blog/b), the key changes and React remounts it: Client Component state is reset, the DOM is recreated and effects run again. Deeper navigations don't remount a higher template, and search-param-only changes remount nothing.
Use a template when something should start fresh per page while living above it: an enter animation, an effect that must re-run for each page, or form state to clear. Often a key on one component is enough (<Form key={id} />); a template keys the whole subtree.
With Cache Components (cacheComponents: true) there's a second layer. Next.js hides the route you leave with React <Activity> instead of unmounting it, and keeps up to three. Returning by <Link> or Back brings the old template instance back, state included: the template still resets on a first visit, not on a return. To reset on every fresh push while keeping Back/Forward restoration, the docs recommend keying on useRouter().bfcacheId.