Exploring the Link component in NextJs: objects as hrefs, disabling prefetching and page scroll, opening in new tab

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!

Link is maybe the most used building component from NextJs. This article aims to explore some of its most known (and less unknown) features.

So, let's start by seeing it in action. First, we will need to import it from next/link and use its href attribute to allow the user to navigate to different pages.

import Link from 'next/link'
export default ()=> {
    return (<nav>
        <Link href="/home"><a>Index</a></Link>
        <Link href="/contact"><a>Contact</a></Link>
    </nav>)
}

NextJs using objects as hrefs for a Link

One lesser-known fact about the href is that you can also send it a javascript object as a parameter. This provides a nicer separation between the actual path and query parameters:

const obj1 = {
    pathname: '/blog/[slug]',
    query: { slug: 'my-post' }
}
const obj2 = {
    pathname: '/about',
    query: { name: 'test' }
}

// will link to /blog/my-post
<Link href={obj1}>Link1</Link>

// will link to /about?name=test
<Link href={obj2}>Link2</Link>

NextJs remove the prefeching for a Link

One huge performance advantage of using the Link component is its prefeching capability for the linked local pages. If you want to reduce the data usage and disable prefetching you can say:

<Link href={url} prefetch={false}>
    <a>Open link</a>
</Link>

However keep in mind that prefetching will still take place on the hover event for the Link elements, even if prefetch is set to false.

Links in NextJs using replace and scroll

Two other properties that allow extra customization are the replace and scroll attributes. The first one will replace the current url in the browser history (the back button will not work). While the second one will prevent scrolling to the target page top, or hash id if provided:

<Link href={url} replace scroll={false}>
    <a>Goto link</a>
</Link>

NextJs open a Link in a new tab

And a final tip. If you want to open a <Link /> in a new tab you can do:

<Link href={url}>
  <a target="_blank" rel="noopener">Open this in a new tab</a>
</Link>

Be sure to take a look at the official documentation and if you are wondering why we need to place an <a> inside of an <Link> I've written another article about this here.

And speaking of opening links in new tabs don't forget to include the rel="noopener" in order to avoid any security vulnerabilities.

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

Neural Networks for JavaScript developers
Presale - 15$ free coupon

Hi friend! Before you go, just wanted to let you know that in March 2023 I will be launching the TensorFlow.Js by Example course.

This course will include basic concepts of AI and Neural Networks done with TensorFlowJs such as:

  • Working with datasets
  • Visualizing training
  • Deep Neural Networks in JavaScript
  • Reinforcement Learning
  • Convolutional neural networks
  • and much more ...

Also, there will be a lot of examples as:

  • Object detection
  • Natural language processing
  • Face and voice recognition
  • Gaming AI

Join now the waiting list and get a 15$ coupon off the launching price!

X