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>
</>
)
}
"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 valueFrom early docs
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 theas
when "as" is helpful
Let's say you have a
DynamicPostComponent
and you are passingpost
propInside this component, you would tell next.js where to go in pages directory. so you use
href
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 useas
Your Link compnent will look like this
as
is removed after v10. https://nextjs.org/blog/next-10