DEV Community

Cover image for How to get the id of a dynamic route with Nextjs getServerSideProps
off.tokyo
off.tokyo

Posted on

How to get the id of a dynamic route with Nextjs getServerSideProps

Things to do

Normally, in order to get the id 1 in Next.js dynamic routing (post/1), you can write the following to get the value.

const { id } = router.query;
Enter fullscreen mode Exit fullscreen mode

How do I get this in getServerSideProps?

export async function getServerSideProps(context) {
Enter fullscreen mode Exit fullscreen mode

You can do the following to get it.


export async function getServerSideProps(context) {
  const { id } = context.query;
Enter fullscreen mode Exit fullscreen mode

Reference.

Dynamic routing with getServerSideProps in Nextjs

How to get the id of dynamic routing with getServerSideProps in Nextjs

Top comments (0)