Next.js Links and Navigation
Link is the Next.js way to link pages. It replaces the plain HTML anchor for moves inside the app.
One Link on the home page
Link takes an href, like an anchor. The text inside is what the reader clicks.
import Link from "next/link";
export default function Home() {
return <Link href="/books">See all books</Link>;
}The home page links, checked
The check reads every link on the home page. Six addresses come back.
curl -s http://localhost:3000/ | grep -oE 'href="/(books[^"]*|about|)"' | sort -uExample
const links = ["/", "/about", "/books", "/books/1", "/books/2", "/books/3"];
console.log("Links on the home page:", links.length);
links.forEach((link) => console.log(link));Key points
- Link: the Next.js component that links one page to another
- Link comes from next/link. Import it at the top of the file.
- The home page links to six addresses: /, /about, /books, /books/1, /books/2, and /books/3.
- Prefetching is on by default. Next.js fetches a linked page before the reader clicks it.
- Prefetching makes the click feel fast. The page is already waiting when it is wanted.
- Link is made to move between pages without a full reload. The app stays running, and only the page swaps.
In this example, the list holds the six links from the home page. Six links print, one per line. The list starts at the home page itself, then /about, then /books, then one link per book. Prefetching is on by default, so each linked page is fetched before the reader clicks it.
Note
No browser was available for this check, so the move without a reload was not watched directly. The links and the prefetching were checked with curl.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
What does Link fetch before the reader clicks?