Next.js Discord

Discord Forum

Using NextJS 13 App Router, how do I access the query params in the request, on server side?

Answered
Komondor posted this in #help-forum
Open in Discord
Avatar
KomondorOP
I don't know what I'm missing here. I can't find any documentation that shows how to access the query params in the request from within the Page component on server side.
Answered by Komondor
I have this
export default async function Page(req) {

  const { searchParams } = new URL(req.url)

  const token = searchParams.get('token')

  
  return (
    <div>hello</div>
  )
}

but am receiving the error TypeError [ERR_INVALID_URL]: Invalid URL
View full answer

8 Replies

Avatar
KomondorOP
I have this
export default async function Page(req) {

  const { searchParams } = new URL(req.url)

  const token = searchParams.get('token')

  
  return (
    <div>hello</div>
  )
}

but am receiving the error TypeError [ERR_INVALID_URL]: Invalid URL
Answer
Avatar
KomondorOP
nvm
Avatar
Barbary Lion
const { searchParams } = new URL(req.url)~

const searchParams = useSearchParams();
Avatar
KomondorOP
This works
export default async function Page({
  params,
  searchParams,
}: {
  params: { slug: string }
  searchParams: { [key: string]: string | string[] | undefined }
}) {

  console.log(searchParams);

  // const token = searchParams.get('token')

  
  return (
    <div>hello</div>
  )
}
Julgers I tried your solution earlier but I get errors about using useSearchParams outside of a client component
Avatar
Barbary Lion
yeah I did not read your question correctly
that is how to do it on client components
what I sent