get href nextjs code example
Example 1: next link
import Link from 'next/link'
function Home() {
return (
<ul>
<li>
<Link href="/">
<a>Home</a>
</Link>
</li>
<li>
<Link href="/about">
<a>About Us</a>
</Link>
</li>
<li>
<Link href="/blog/hello-world">
<a>Blog Post</a>
</Link>
</li>
</ul>
)
}
export default Home
Example 2: nextjs router get complete url
function getFullUrl(req, fallback) {
if(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl
}
else if(!(typeof window === 'undefined')) {
return window.location.href
} else {
return fallback
}
}
static async getInitialProps({req}) {
let fullUrl = getFullUrl(req, "")
return { fullUrl: fullUrl }
}