#Inside of generateMetadata how can I get the page url

1 messages · Page 1 of 1 (latest)

orchid minnow
#

Inside of generateMetadata how can I get the page url

meager nebulaBOT
#

Hi @orchid minnow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The generateMetadata function in Next.js does not directly provide the URL of the page. It receives the parameters of the current route in the props object, which includes params and searchParams. The params object contains the dynamic route parameters from the root segment down to the segment generateMetadata is called from. The searchParams object contains the current URL's search params.

Here's an example of how these parameters look:

generateMetadata({ params, searchParams }) {
  console.log(params); // dynamic route parameters
  console.log(searchParams); // URL's search params
}

However, if you need the full URL, you might need to construct it manually using these parameters and your base URL. The base URL can be obtained from the metadataBase field in the Metadata object. If not configured, metadataBase has a default value of <https://${process.env.VERCEL_URL}> or <http://localhost:${process.env.PORT> || 3000} if VERCEL_URL is not detected.

Please note that this approach might not work for all use cases, especially for static routes. There's an open issue on GitHub discussing the limitations of generateMetadata and the need for a function equivalent to usePathname() that can work in Server Components.

Sources:


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