1import Link from "next/link"; //import this
2
3<Link href="/"> //wrap a tag with Link
4 <a>Home</a>
5</Link>
6
7<Link href="https://nextjs.org"> //external page
8 <a>Next.js</a>
9 </Link>
10
11
12
13
14
15
1import { useRouter } from 'next/router'
2
3function ActiveLink({ children, href }) {
4 const router = useRouter()
5
6 const handleClick = (e) => {
7 e.preventDefault()
8 router.push(href)
9 }
10
11 return (
12 <a href={href} onClick={handleClick} style={style}>
13 {children}
14 </a>
15 )
16}
17
18export default ActiveLink
19
1function getFullUrl(req, fallback) {
2 //server side request object(req)
3 if(req) {
4 return req.protocol + '://' + req.get('host') + req.originalUrl
5
6 } //making sure we are on the client side
7 else if(!(typeof window === 'undefined')) {
8 return window.location.href
9
10 } else {
11 return fallback
12 }
13}
14
15//usage on nextjs
16static async getInitialProps({req}) {
17 let fullUrl = getFullUrl(req, "")//gets the full url or fallback to ""
18 return { fullUrl: fullUrl }
19}