Next.js Discord

Discord Forum

when use `Metadata`, how can I add the pathname dynamically?

Answered
Egyptian Mau posted this in #help-forum
Open in Discord
Egyptian MauOP
Hello,

I am using Metadata from Nextjs for the Head Component, now I wanted to use the pathname dynamically based on the route I am at, .e.g. / for Index, /about for About Page, how I can do this dynamically?

in /pages I can use useRouter() to do it, how I can achieve it in App Directory?

25 Replies

Giant panda
Also since your metadata object is tied to a page you already know its path (unless you move it around)
Egyptian MauOP
move it around means? @Giant panda
Giant panda
Tell us first what you want to do in the first place
Typically it's highly discouraged trying to get the current pathname in server components or on the server in general
So if you think you need that it's likely you don't
Egyptian MauOP
you're referring that if I am using Metadata in all the pages specifically then no need to get the pathname dynamically, just give static way, like /about, /contact, right?
Giant panda
I mean that I have a file at app/collection/page.tsx and my metadata for it just looks like this:
export const metadata = {
  title: 'Collection'
}
So there is no need to attempt to figure out on which page your are dynamically unless you have dynamic paths like /foo/[id]
Egyptian MauOP
but for e.g., we've Blogs page, and Single Blog Page, so for single blog page I want to have twitter & openGraph value dynamically, how can that be achieved?
Giant panda
Answer
Giant panda
Egyptian MauOP
import { Metadata, ResolvingMetadata } from 'next'
 
type Props = {
  params: { id: string }
  searchParams: { [key: string]: string | string[] | undefined }
}
 
export async function generateMetadata(
  { params, searchParams }: Props,
  parent: ResolvingMetadata
): Promise<Metadata> {
  // read route params
  const id = params.id
 
  // fetch data
  const product = await fetch(`https://.../${id}`).then((res) => res.json())
 
  // optionally access and extend (rather than replace) parent metadata
  const previousImages = (await parent).openGraph?.images || []
 
  return {
    title: product.title,
    openGraph: {
      images: ['/some-specific-page-image.jpg', ...previousImages],
    },
  }
}
 
export default function Page({ params, searchParams }: Props) {}


I got this get metadata, then how it'll be used in the Metadata?
Giant panda
This IS the generated metadata
Egyptian MauOP
so then I don't need to pass it Metadata, it'll automatically add it to the meta?
Giant panda
That's the whole point of it, yes...
Egyptian MauOP
Sorry, I don't meant to bother you guys, but this App Router is pretty new & quite confusing for me,
@Giant panda That's the whole point of it, yes...
Egyptian MauOP
still we can use Link component from Nextjs or there's a change for it? like for routing from one page to another using the Link/useRouer component?
Giant panda
This is an entirely different question now. And I am a bit on edge since I don't mind people asking questions but I feel you really should invest way more time in reading the docs thoroughly and maybe a second time if something is not clear. In the case of links there is a literal section called Linking: https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
Egyptian MauOP
still can we still use getServerSideProps(), getStaticProps() and getStaticPaths()
Egyptian MauOP
then, what can be used?
click on the link and read the docs i linked
Egyptian MauOP
I wanted to know that Nextjs App Router is completely server side, so now even to useState what is required for it?