#app routing - get pathname in server components
1 messages · Page 1 of 1 (latest)
Hi @mellow terrace. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
I'm sorry, but according to the information provided in the extracts, it's not possible to get the pathname in Server Components using the usePathname hook. This hook is specifically designed for use in Client Components.
In the app directory, you should use the three new hooks imported from next/navigation: useRouter(), usePathname(), and useSearchParams(). However, these new hooks are only supported in Client Components and cannot be used in Server Components (source (https://nextjs.org/docs/pages/building-your-application/upgrading/app-router-migration)).
Reading the current URL from a Server Component is not supported. This design is intentional to support layout state being preserved across page navigations (source (https://nextjs.org/docs/app/api-reference/functions/use-pathname)).