Next.js Discord

Discord Forum

Access URL in a server component

Unanswered
Bombay posted this in #help-forum
Open in Discord
BombayOP
Is there a way to access the current path inside a server component, so that I can see the query parameters ? I know that "usePathname()" exists, but that requires the "use client". I could also use a middleware, but that would require an extra step.
async function getData() {
  // I need to access query params here to fetch later on
  
}

export default function Page() {
  const info = getData();
  
  return <h1>...</h1>
}

2 Replies

BombayOP
export default function Page({
  params,
  searchParams,
}: {
  params: { slug: string }
  searchParams: { [key: string]: string | string[] | undefined }
}) {
  // Can access search params here
  return <h1>My Page</h1>
}