Newbie here, sorry if this is extremely basic question but I'm not able to wrap my head around which technique should be followed to navigate across various pages.
As of now I know three different methods through which I can achieve this:
next/link
router.push()
using useRouter exported ny next/router<a></a>
<Link href="/about"><a>About me</a></Link>
<a href="/about">About me</a>
<button onClick={() => router.push("/about")}>About me</button>
All of the methods work and achieve the same exact thing. I just wanted to know what are the differences, if any, among these approaches. Thank you so much in advance!
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.
To navigate to a different URL, use router.push . This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.
push by default doesn't trigger a page reload #21787.
Next.js has a file-system based router built on the concept of pages. When a file is added to the pages directory, it's automatically available as a route. The files inside the pages directory can be used to define most common patterns.
router.push('/push')
behaves similarly to window.location. It does not create a <a>
tag, which means - if you are concern with SEO, your links will not be detected by crawlers.
<Link>
However, <Link>
will create a <a>
tag, which means your links will be detected when crawlers scrape your site. Endusers will still navigate with without reloading the page, creating the behavior of a Single Page App.
<a>
<a>
tag without using next/link's <Link>
creates a standard hyperlink which directs end user to the url as a new page. (standard behavior).
You should be using <Link>
throughout all your website, and use router.push for places where you need redirect in order to retain the behaviour of a Single Page App.
Buttons are for actions and links are to go somewhere.
If you are using NextJs, I'm assuming SEO matters to you here.
Consider these before making a decision:
router.push()
is mostly used in an event handler (onClick
here). This is an action. So let's say you click on the button, then you do some task, and based on the result you take the user to another page. Then you'd want to use router.push()
. Don't use it just to go to another page. Note that this is bad for SEO if you want it to be crawled.
<Link>
does some heavy lifting for you and has a bunch of props that you can pass to customize it. This is what you need most of the time to go to another page. When you are using it, the default browser's behavior to reload the whole page(as you'd see with the <a>
tag) is overridden. Crawlers see it as a link to another page, so it's good for SEO.
<a>
tag is just a plain HTML element, with all the default behaviors. When you use it, a full reload happens. If you are using it, try switching to <Link>
. Similar to <Link>
it's good for SEO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With