Part 03 / Topic 5 of 7
Active-link patterns with usePathname
Knowing which link is "active" — matching the page currently on screen — needs the current URL, and <Link> alone doesn't expose that. The usePathname hook (from next/navigation) does, but only inside a Client Component, since reading "what URL is this" is inherently something the browser's current page knows, not something a Server Component's static markup can carry.
app/store/_components/StoreNav.tsx:
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
const links = [
{ href: "/store", label: "Home" },
{ href: "/store/products", label: "Products" },
{ href: "/store/about", label: "About" },
{ href: "/store/contact", label: "Contact" },
];
export function StoreNav() {
const pathname = usePathname();
return (
<nav style={{ display: "flex", gap: "1rem", padding: "1rem 2rem 0", fontFamily: "system-ui" }}>
{links.map((link) => {
const isActive = link.href === "/store" ? pathname === "/store" : pathname.startsWith(link.href);
return (
<Link
key={link.href}
href={link.href}
aria-current={isActive ? "page" : undefined}
style={{ fontWeight: isActive ? 700 : 400, textDecoration: isActive ? "underline" : "none" }}
>
{link.label}
</Link>
);
})}
</nav>
);
}
Two details earn their own line. First, /store's check must be an exact match (pathname === "/store"), not startsWith, because every other link's href also starts with /store — without the exception, "Home" would read as active on every single storefront page. Second, every other link uses startsWith on purpose, so a page two levels deeper than its nav entry — /store/products/p1 — still marks "Products" active, not nothing.
Both claims are real, curled output, not assumed:
$ curl -s http://localhost:PORT/store/products/p1 | 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 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>
$ 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/about">About</a><a style="font-weight:400;text-decoration:none" href="/store/contact">Contact</a></nav>
On /store/products/p1, "Products" carries aria-current="page", not "Home" — proving the nested-match rule. On /store itself, "Home" carries it instead, proving the exact-match exception. This works on the very first server-rendered response, before any client-side hydration, because usePathname can read the pathname from the server-rendered URL on the initial request too — it is not a client-only trick for this proof, even though the hook itself only runs in a Client Component.
Full file: examples/02-store-nav-active-link.tsx.
Done here? The practice set for Part 3 — including the solutions — is on the part page.