✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Discussion about passing parameters in Next.js getStaticPaths and getStaticProps

61 replies
Last updated: Sep 8, 2022
0

I am trying to return in my params the values of the

slug
and
tags
, please notice that
tags
is not an array it's just a string.
export async function getStaticPaths() {
  const chapters = await client.fetch(
    `*[_type == "chapters" && defined(slug.current)] {
      "slug": slug.current,
      "tags": tags
    }`
  );
  return {
    paths: chapters.map((s : any) => ({params: {slug : s.slug,tags: s.tags}})),
    fallback: false,
  }
}
So when I try to get the
tags
value in my getStaticProps I get a null value, yet
slug
is not null.
export async function getStaticProps(context: any) {
  const { slug = "",tags="" } = context.params
  const suggestions = await client.fetch(`
  *[_type == "chapters" && tags == $tags][0]
`, { tags })
when I try to replace the
tags
value in getStaticProps with a defined value , it fetches the document successfully ,this means the
tags
value received from getStaticPaths is null. How can I solve this issue ?
Sep 7, 2022, 12:14 PM
What is the path to that file in your project? Because that’s how we can figure out which params should be provided.
Sep 7, 2022, 12:19 PM
it's
/[slug]
Sep 7, 2022, 12:20 PM
That‘s it? Nothing else? Just
/pages/[slug].js
?
Sep 7, 2022, 12:21 PM
yes exactly !
Sep 7, 2022, 12:22 PM
But then where do you expect
tags
to come from exactly?
Sep 7, 2022, 12:23 PM
Because
context.params
contains your routing parameters. In that route of yours, there is only
slug
. Not
tags
.
Sep 7, 2022, 12:23 PM
oh
Sep 7, 2022, 12:23 PM
so how can I pass the
tags
as well ?
Sep 7, 2022, 12:24 PM
/[slug]/[tags].js
or something.
Sep 7, 2022, 12:24 PM
No, my idea is to get recommendation chapters in the same page !
Sep 7, 2022, 12:24 PM
so I have to fetch the exact same
tags
of all the document
Sep 7, 2022, 12:25 PM
getStaticPaths
is there to generate all the possible paths for the current route. Typically, if you have a route with a dynamic
slug
parameter, then your
getStaticPaths
function needs to return all the existing slugs so that Next.js can statically build all your pages.
Sep 7, 2022, 12:26 PM
I understand
Sep 7, 2022, 12:27 PM
Do you have any idea how possibly I can manipulate the code to receive
tags
values as well
Sep 7, 2022, 12:28 PM
There is not 2 ways: you need to have it as part of your path. That’s how Next.js work.
Sep 7, 2022, 12:30 PM
Otherwise, you may use the same page for both slugs and tags, but I wouldn’t recommend it because you could have conflicts. Essentially flatten both collections into one and treat it as a unique collection.
Sep 7, 2022, 12:31 PM
by collections you mean the multiple documents of
chapters
?
Sep 7, 2022, 12:31 PM
export async function getStaticPaths() {
  const chapters = await client.fetch(
    `*[_type == "chapters" && defined(slug.current)] {
      "slug": slug.current,
      "tags": tags
    }`
  )
  return {
    paths: chapters
      .map(chapter => [
        { params: { slug: chapter.slug } },
        { params: { slug: chapter.tags } },
      ])
      .flat(),
    fallback: false,
  }
}
Sep 7, 2022, 12:32 PM
the same problem !
Sep 7, 2022, 12:36 PM
Check the data from the query. If
tags
is null, it’s because it’s missing from your document maybe.
Sep 7, 2022, 12:38 PM
I tested the query it works perfectly
Sep 7, 2022, 12:42 PM
are you sure about this part ?
Sep 7, 2022, 12:43 PM
  { params: { slug: chapter.slug } },
  { params: { slug: chapter.tags } },
Sep 7, 2022, 12:43 PM
No because I’m still not exactly sure how your data works. The last version I sent treats both slugs and tags as potential slugs.
Sep 7, 2022, 12:44 PM
when I map the
chapters
query :
 chapters.map((s) =>{
  console.log(s.slug, s.tags)
 })

Sep 7, 2022, 12:49 PM
I get this
Sep 7, 2022, 12:49 PM
slug , tags values
Sep 7, 2022, 12:49 PM
each
slug
corresponded to a
tags
value
Sep 7, 2022, 12:50 PM
and that's why I want to send the
tags
value of each
slug
to the getStaticProps so I can fetch the documents
Sep 7, 2022, 12:51 PM
Right, and you want to be able to access a given page with either its slug or its tags, right?
Sep 7, 2022, 12:51 PM
right
Sep 7, 2022, 12:51 PM
it slug
Sep 7, 2022, 12:52 PM
Wait.
Sep 7, 2022, 12:52 PM
Only via the slug?
Sep 7, 2022, 12:52 PM
yes only via the slug
Sep 7, 2022, 12:52 PM
is it possible ?
Sep 7, 2022, 12:52 PM
Sure, but then I don’t get why you need tags at all.
Sep 7, 2022, 12:52 PM
In
getStaticProps
, you receive the slug and you can use it to query the right document.
Sep 7, 2022, 12:53 PM
Why do you need tags?
Sep 7, 2022, 12:53 PM
I have to use the tags so I can fetch a recommendation chapters in the UI
Sep 7, 2022, 12:53 PM
something lie : You might also like
Sep 7, 2022, 12:53 PM
Are
tags
shared across chapters?
Sep 7, 2022, 12:53 PM
yes ofc
Sep 7, 2022, 12:53 PM
AAaaaaaah.
Sep 7, 2022, 12:53 PM
Okay I finally get it.
Sep 7, 2022, 12:53 PM
Alright.
Sep 7, 2022, 12:53 PM
all chapters have tags
Sep 7, 2022, 12:53 PM
export async function getStaticProps(context: any) {
  const { slug } = context.params
  const chapter = await client.fetch(
    `*[_type == "chapters" && slug.current == $slug][0]`,
    { slug }
  )

  if (!chapter) return { notFound: true }

  const suggestions = await client.fetch(
    `*[_type == "chapters" && tags == $tags]`,
    { tags: chapter.tags }
  )

  return { props: { chapter, suggestions } }
}
Sep 7, 2022, 12:55 PM
So first you query the chapter document based on its slug, and then you query the related chapters based on the tags from the chapter.
Sep 7, 2022, 12:56 PM
AAAA
Sep 7, 2022, 12:57 PM
works magically !!
Sep 7, 2022, 12:57 PM
Thank you so much
Sep 7, 2022, 12:57 PM
Amazing. 😄
Sep 7, 2022, 12:58 PM
Took a while but we got there.
Sep 7, 2022, 12:58 PM
Yeah, I have to be more specific the next time
Sep 7, 2022, 1:00 PM
I'm glad we found a way. 🙂
Sep 7, 2022, 1:01 PM
By the way how can I filter the documents to fetch all the same
tags
but not the current one !
Sep 7, 2022, 1:13 PM
*[ _type == "chapters" && tags == $tags && slug.current != $slug ]
Sep 7, 2022, 1:17 PM
ah yeah I was trying
!==
instead
Sep 7, 2022, 1:17 PM
thanks.
Sep 7, 2022, 1:17 PM
hello
user B
🙂Could you try and use threads for your questions please, this way it is easier to help everyone and not spend time on things that are posted in multiple places and multiple times
🙏And wonderful
user F
was able to help you! 🥳
Sep 8, 2022, 4:59 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?