Get the current pathname from server-side
Answered
Bigheaded ant posted this in #help-forum
Bigheaded antOP
How can I get the current pathname from the server-side? Is it possible to get it? because from what I've already searching, the only way to do it is to use hooks
usePathname but this only run in the client-side.Answered by Northeast Congo Lion
import { headers } from 'next/headers';
export default function Navbar() {
const headersList = headers();
const domain = headersList.get('host') || "";
const fullUrl = headersList.get('referer') || "";
console.log(fullUrl);
}9 Replies
Northeast Congo Lion
import { headers } from 'next/headers';
export default function Navbar() {
const headersList = headers();
const domain = headersList.get('host') || "";
const fullUrl = headersList.get('referer') || "";
console.log(fullUrl);
}Answer
Northeast Congo Lion
use next/ headers to get the host and referrer
@Bigheaded ant
@Northeast Congo Lion
import { headers } from 'next/headers';
export default function Navbar() {
const headersList = headers();
const domain = headersList.get('host') || "";
const fullUrl = headersList.get('referer') || "";
console.log(fullUrl);
}
Bigheaded antOP
I have already tried this sir, but we should set the host in the middleware first, right?
Japanese flying squid
Can you give us better explanation in what case do you need it exactly? Then I think we can be more helpful
@Japanese flying squid Can you give us better explanation in what case do you need it exactly? Then I think we can be more helpful
Northeast Congo Lion
thats alot of info aklready
Japanese flying squid
I mean like on server side, why do you need to get pathname? from routes, you already know what’s pathname, and if you have something dynamic, you just can get it from page or layout params on server side, am I wrong?