Question presented to candidate:
"In a Next.js app, what actually happens when a user clicks a <Link>, compared to a normal <a> tag? And when would you use useRouter instead?"
What a strong answer should cover:
<Link>renders a real<a href>but intercepts the click: no new document is loaded, the URL is updated with the History API, and only the route segments that changed are re-rendered; shared layouts and their state stay.- The data for the new route is the RSC payload, fetched from the server or taken from a prefetch.
- Links are prefetched in production when they enter the viewport: static routes fully, dynamic routes only partly (the shared parts and any
loading.tsx). useRouter()fromnext/navigation(notnext/router) does the same from code:push,replace,back,forward,prefetch,refresh; it only works in Client Components.router.refresh()re-renders Server Components for the current route without losing client state; it does not invalidate server caches.- A plain
<a>, or a link to a route under a different root layout, is a full page load.
Clarifying questions expected:
- "Does the navigation happen after an event, like a form submit?" β that is the case for
useRouter; otherwise prefer<Link>. - "Are the target routes static or dynamic?" β decides how much prefetching helps and whether a
loading.tsxis needed.
Code / implementation expected: Yes β a nav with active links, a Link with prefetch={false} and replace, and a button that calls router.refresh() and router.push().