Next.js Discord

Discord Forum

Get the current pathname from server-side

Answered
Bigheaded ant posted this in #help-forum
Open in Discord
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);
}
View full answer

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
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
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?