next router get complete url code example
Example: nextjs router get complete url
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 }
}