Understanding Navigation Methods in Next.js: Link Component vs Router vs `<a>`</a>
When building a Next.js application, you'll often need to navigate between pages. Next.js provides multiple ways to handle this, including:
- The
Link
component (fromnext/link
) router.push()
(fromnext/router
)- Standard HTML
<a>
tags
Each of these methods can accomplish the same thing—navigating between pages—but there are key differences that affect performance, user experience, and how the page loads. Let’s break down the pros and cons of each.
1. Link
Component (from next/link
)
<Link href="/about">
<a>About me</a>
</Link>
Key Features:
- Client-Side Navigation: The
Link
component is optimized for Next.js’s single-page app (SPA) experience, meaning transitions between pages happen without a full page reload. - Preloading: The
Link
component automatically preloads the target page when it's in the viewport or hovered over. This makes transitions faster because Next.js loads the page in the background. - Accessibility: The component wraps the content in an
<a>
tag, ensuring that it remains accessible and SEO-friendly.
When to Use:
Use the Link
component when you want to optimize performance with client-side transitions and preload pages in advance.
2. router.push()
(from next/router
)
const router = useRouter();
return (
<button onClick={() => router.push("/about")}>About me</button>
);
Key Features:
- Programmatic Navigation:
router.push()
is useful when you need to navigate programmatically, such as after a form submission, button click, or based on some condition. - Client-Side Navigation: Similar to the
Link
component, this method also performs client-side navigation, ensuring a smooth transition between pages without a full page reload. - More Control: This method allows for greater flexibility, as you can execute logic before or after navigating. For example, you could display a loading spinner or validate input before changing pages.
When to Use:
Use router.push()
when you need conditional or programmatic navigation—for example, when navigating after a specific event like form submission or based on user actions.
3. Standard <a>
Tag
<a href="/about">About me</a>
Key Features:
- Full Page Reload: Unlike the previous methods, using a simple
<a>
tag will trigger a full page reload, which is the traditional web behavior. - No Preloading: Since this doesn’t use any Next.js optimizations, there’s no preloading of the target page, leading to slower transitions.
- SEO-Friendly: Like the other methods, the
<a>
tag is SEO-friendly, but it's less efficient for navigation within a Next.js app.
When to Use:
Use the <a>
tag for external links or when a full page reload is necessary. For internal navigation within a Next.js app, this is generally not recommended as it can negatively impact performance.
Comparison Overview
Here’s a quick comparison of the three methods:
Feature | Link (from next/link ) | router.push() | Standard <a> Tag |
---|---|---|---|
Navigation Type | Client-side navigation (SPA) | Client-side navigation (SPA) | Full page reload (MPA behavior) |
Automatic Preloading | Yes | No | No |
Accessibility | SEO-friendly | SEO-friendly | SEO-friendly |
Use case | Standard navigation | Programmatic, conditional | Traditional, external links |
Performance | Fast (with preloading) | Fast (without preloading) | Slower (full page reload) |
Key Takeaways
- Use
Link
: Ideal for internal navigation within a Next.js app, where preloading and client-side transitions improve performance and user experience. - Use
router.push()
: When you need programmatic navigation or need to handle navigation based on user actions or conditions. - Use
<a>
: When linking to external websites or when a full page reload is explicitly needed.
For most internal links within a Next.js app, Link
or router.push()
will give you the best performance and user experience, while the standard <a>
tag should be reserved for external links or cases where you want traditional web behavior.