Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using a tag in nextjs Link?

Discovering NextJs, I am wondering why the a tag is needed when creating a link. Following the tutorial we have this example:

<Link href="/"><a>Back to home</a></Link>

But the link works even whitout the a tag like <Link href="/">Back to home</Link>.

What is the purpose of this a tag? Isn'it redundant with the Link component?

like image 303
Aurélien Avatar asked Aug 09 '20 09:08

Aurélien


People also ask

When should I use NextJS link?

When linking between pages on websites, you use the <a> HTML tag. In Next. js, you can use the Link Component next/link to link between pages in your application. <Link> allows you to do client-side navigation and accepts props that give you better control over the navigation behavior.

How do I use an external link on NextJS?

To add an external link to the Next. js Link component we should use attribute passHref. This attribute is set to false by default. This attribute forces Link to send the href property to its child.

How do I add a body tag to NextJS?

If you want to add a class ( className ) to the body tag of a a NextJS page, you'll need to add it via JavaScript in the useEffect (or componentDidMount if you're using class-based components) Lifecycle function.


1 Answers

It's for semantic html and SEO

This gives your rendered html proper semantics. This will help with your sites over all SEO. You can see examples and full explanation in the docs here.

If the child of Link is a custom component that wraps an tag, you must add passHref to Link. This is necessary if you’re using libraries like styled-components. Without this, the tag will not have the href attribute, which might hurt your site’s SEO.

like image 158
Joe Lloyd Avatar answered Sep 27 '22 20:09

Joe Lloyd