2

Can someone explain to me the difference between using passHref and using an anchor tag when setting up links between pages in Next.js?

E.g. what is the difference between:

<Link href={`/posts/${post.id}`} passHref>
  <h2>
    {post.id} {post.title}
  </h2>
</Link>

and

<Link href={`/posts/${post.id}`}>
  <a>
    {post.id} {post.title}
  </a>
</Link>

I tried reading about it in the documentation but could not seem to find the answer.

Elco
  • 97
  • 1
  • 7

1 Answers1

0

From the docs:

passHref - Forces Link to send the href property to its child. Defaults to false

When using <Link> wrapping a custom component and not an anchor tag, the <Link> tag must have the passHref attribute. Without this, the tag will not have the href attribute, which hurts your site's accessibility and might affect SEO.

Source

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39