javascriptnext.jspathrouterreact-server-components

How can I get the url pathname on a server component next js 13


So basically I have a server component in app dir and I want to get the pathname. I tried to do it using window.location but it does not work. Is there any way I can do this?


Solution

  • This works for me

    import { headers } from "next/headers";
    
    const headersList = headers();
    const domain = headersList.get("x-forwarded-host") || "";
    const protocol = headersList.get("x-forwarded-proto") || "";
    const pathname = headersList.get("x-invoke-path") || "";