#app routing - get pathname in server components

1 messages · Page 1 of 1 (latest)

mellow terrace
#

using app routing, how can i get the pathname in server components?

frosty fogBOT
#

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)).

If you need to access the pathname in a Server Component, you might need to pass it as a prop from a Client Component or find another workaround.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: