nextjs router get complete url
import { useRouter } from 'next/router'
function ActiveLink({ children, href }) {
const router = useRouter()
const handleClick = (e) => {
e.preventDefault()
router.push(href)
}
return (
<a href={href} onClick={handleClick} style={style}>
{children}
</a>
)
}
export default ActiveLink
function getFullUrl(req, fallback) {
//server side request object(req)
if(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl
} //making sure we are on the client side
else if(!(typeof window === 'undefined')) {
return window.location.href
} else {
return fallback
}
}
//usage on nextjs
static async getInitialProps({req}) {
let fullUrl = getFullUrl(req, "")//gets the full url or fallback to ""
return { fullUrl: fullUrl }
}
Also in JavaScript:
- js create object from array
- react arrays
- clear formarray in angular 6
- google script get date without time
- node js async delay
- routes in node js
- random item from array javascript
- javascript change class of item
- javascript subtract days from date
- luxurious
- invalid left-hand side in assignment
- passport jwt npm
- "ERR_SSL_WEAK_EPHEMERAL_DH_KEY"
- looping in javascript
- ctx beginpath react
- grpc node
- how to move div using jquery
- useful javascript code snippet for console
- js array as parameter
- ajax submit form data
- javascript atualize array
- nodejs format text
- how to manage logging using winston for production and development in node js "github"
- jquery continue each loop