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
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
but am receiving the error
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
8 Replies
KomondorOP
I have this
but am receiving the error
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
KomondorOP
nvm
@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`
Barbary Lion
const searchParams = useSearchParams();
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@Komondor Julgers I tried your solution earlier but I get errors about using `useSearchParams` outside of a client component
Barbary Lion
yeah I did not read your question correctly
that is how to do it on client components
what I sent