How to access both the params and query string from a next.js Link in getServerSideProps?

Matt Heslington

I have a component that links to a NextJS page in which I'm using getServerSideProps and need to access the params and the query string.

I really would like to have a 'clean' url such as 'domain.com/usersname/likes' without the '?userId=h337dhdh37dhdhd' on the end. Is it possible like this?

<Link href={`/${user?.username}/likes?userId=${id}`} as={`/${user?.username}/likes`}>
    ...
</Link>

In my getServerSideProps I'm using context as per the Next documentation:

export async function getServerSideProps(context) {
    const username = context.params.username;
    const userId = context.query.userId;
    ...
}

The params (username) are fine, but whatever I do the userId is always undefined. Can anyone see what I'm doing wrong, please?

EDIT: codesandbox link: https://codesandbox.io/s/nifty-bush-livm7 - based on Restricted Beam's answer below

Any help very much appreciated, Matt

restricted-beam

If your goal is to have a clean URL without the '?query=something' , you should try the slug and dynamic routes feature of nextjs. For using this feature:

  • You need to change the file name to [...slug].js and all the different parameters in the link can be caught in an array.

So for "domain/usersname/likes/userId", your [...slug].js file will be in the root directory and to access its value you will use context like the following:

export async function getServerSideProps(context) {
    const username = context.params.slug[0];
    const userId = context.params.slug[2];
    ...
}

So, if your user visits the link: domain/user-XYZ/likes/12345abc, the slug array would be like:

context.params.slug:["user-XYZ","likes","12345abc"]

Docs: https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to access route parameter inside getServerSideProps in Next.js?

passing id through Link to getServerSideProps :next js

How can I use "useRouter" to get params in the url inside getServerSideProps() in Next.js

NEXT JS - How to remove Query Params?

"query is not a function" Next Js getServerSideprops and firebase error

access both params and query with zod

How to call getServerSideprops with useEffect in Next.js

Delete empty params from query string JS

How to retrieve Id from URL (query params) in Next.js 13 client component

In Next.js 13 with turbopack, how do I access cookies without getServerSideProps?

How to handle multiple dehydrated queries using react-query in next JS getServersideProps

Next js not running getServerSideProps

How to remove duplicate slashes from URL in Next Js.How to intercept and modify a request on the server side getServerSideProps?

Access data returned by getServerSideProps inside Layout in Next.js

Next.js using getServerSideProps how do i pass props from page to components?

Keep the same pathname in Link, only add query params in Next.JS

Next.js Types not being infered from getServerSideProps to NextPage

How to get full browser url in next js, getServerSideProps

How do I get all the query string params from a request?

How to prevent getServerSideProps from running again when pushing query param?

How can I get (query string) parameters from the URL in Next.js?

How to pass a big data object to another page with dynamic route in next js (without query params)?

Auth0 Next.js error withPageAuthRequired You should not access 'res' after getServerSideProps resolves

getServerSideProps in _app.tsx in Next.js

Preloading getServerSideProps data with Next.js?

Next.js getServerSideProps always undefined

Internal API fetch with getServerSideProps? (Next.js)

Next.js getServerSideProps | Strange behavior for a function

next js console about getServerSideProps props