Is There Any Way to Pass Both ID and Slug from getStaticPaths to getStaticProps [nextjs]

Viewed 1439

I want to set my URL's as slugs in my next.js app, but also need to pass the itemID to getStaticProps in order to get the data I need from my API.

How can I pass both ID and slug from getStaticPaths to getStaticProps rather than one or the other?

export async function getStaticPaths() {
  const paths = items.map(item => (
    { params: { id: item.itemID }}
  ))

  return {paths, fallback: false}
}

I was hoping to do something like this:

const paths = items.map(item => ({ 
      params: { 
        id: item.itemID,
        title: item.description
      }
    }))
1 Answers

I believe you can find the answer to your question in this blog example from Next.js. They call getStaticPaths() first to get a list of all paths, and then pass that information over to getStaticProps() to get data for each of the paths. Since the data is generated at build time, I believe Nextjs gets all your pages built and ready to serve in one go.

Related