How to add params an array of values inside getStaticPaths

Viewed 24

I have a page it structure is the following : /read/[slug]/[number] I want to get the slug value that corresponds each number in the getStaticPaths This is the code :

export async function getStaticPaths() {
  const slugs = await client.fetch(
    `*[_type == "comic" && defined(slug.current)][].slug.current`
  )
  
  const paths = await client.fetch(
    `*[_type == "comic"][].chapters[].number`
  )

  return {
    paths: paths.map((number : any) => ({params: {number: number, slug: "test"}})),
    fallback: false,
  }
  
}

the slugs variable is the array that contains all the slug values. instead of test in the params slug value , I want to get it corresponding value of number , how I can possible manipulate this code to solve it ?

1 Answers

you just need to map on both arrays in the same time, something like this will do it:

paths
    .map((number: any) =>
      slugs.map((slug: any) => ({ params: { number, slug } })))
    .flat(); // to avoid nested arrays

one thing though, I recommend to avoid the any type since it defeats the purpose of using typescript

Related