1

Both links work exactly the same, do we really need to use the as, can't we use just the href?

import Link from 'next/link'

export default function () {
  return (<>
    <Link href="/someroute">
      <a>WithOUT as</a>
    </Link>

    <br />
    <br />

    <Link href="/[param]" as="/someroute">
      <a>With as</a>
    </Link>
  </>
  )
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Paulo Fernando
  • 3,148
  • 3
  • 5
  • 21

1 Answers1

2

"as" is used to have a nicer url. for example, since you are in a dynamic route, that param can be something very crazy, maybe a mongodb id or any hash value

 // mongodb id
 /507f191e810c19729de860ea

 // maybe ipfs hash
 /mtwirsqawjuoloq2gvtyug2tc3jbf5htm2zeo4rsknfiv3fdp46a

From early docs

href is a file system path used by the page and it shouldn't change at runtime. as on the other hand, will be dynamic most of the time according to your needs.

So when next.js navigates, to decide which page to render, it will look at the href but to decide which url to show the user, it will look at the as

when "as" is helpful

Let's say you have a DynamicPostComponent and you are passing post prop

 const DynamicPostComponent = ({post}) => {}

Inside this component, you would tell next.js where to go in pages directory. so you use href

  // assume we have "pages/posts/[slug]" directory
  href="/posts/[slug]"

now inside the page you need to read the id of blog so that you have to fetch the post to show the user. for this you use as

 as={`/posts/${post.slug}`

Your Link compnent will look like this

 <Link href="/postss/[slug]" as={`/posts/${post.slug}`}>
    <a>
     
    </a>
  </Link>

 

as is removed after v10. https://nextjs.org/blog/next-10

Automatic Resolving of href: The as property is no longer needed on next/link

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • Hello Yilmaz, thanks for your answer, but I was not able to reproduce that behavaiour: https://stackblitz.com/edit/nextjs-y3edah?file=pages/[param].js i tried 3 different ways, all the same result, you can click on upper right Open in new Tab to see the URL changing – Paulo Fernando Dec 18 '22 at 15:56
  • I got your point. I updated the answer – Yilmaz Dec 18 '22 at 16:24
  • I think i am starting to understand, is there a way to use the crazy ID? it seems that it's just being lost, because router.query always return `{"param":"someroute"}` – Paulo Fernando Dec 18 '22 at 16:29
  • @PauloFernando next.js will read the "as" – Yilmaz Dec 18 '22 at 16:43
  • @PauloFernando I edited the answer to be more descriptive – Yilmaz Dec 18 '22 at 17:00
  • Yilmaz, would you example be like [this](https://stackblitz.com/edit/nextjs-y3edah?file=pages/index.js)? It seems that you can just use the href, without the as. I even changed the name of the file for [slugX] and stills work. What i see is that if you use as, next will ignore the href and use the as, so why don't just put the route in the href? – Paulo Fernando Dec 18 '22 at 17:46
  • @PauloFernando as is no longer is used. That is why I mentioned early docs. i think after version 10 removed. – Yilmaz Dec 18 '22 at 20:50
  • 1
    sorry, I should have mentioned it, i was watching the world cup, https://nextjs.org/blog/next-10 – Yilmaz Dec 18 '22 at 20:53