Discussion about passing parameters in Next.js getStaticPaths and getStaticProps
61 replies
Last updated: Sep 8, 2022
Y
0
I am trying to return in my params the values of the
So when I try to get the
when I try to replace the
I am trying to return in my params the values of the
slugand
tags, please notice that
tagsis 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, } }
tagsvalue in my getStaticProps I get a null value, yet
slugis not null.
export async function getStaticProps(context: any) { const { slug = "",tags="" } = context.params const suggestions = await client.fetch(` *[_type == "chapters" && tags == $tags][0] `, { tags })
tagsvalue in getStaticProps with a defined value , it fetches the document successfully ,this means the
tagsvalue received from getStaticPaths is null. How can I solve this issue ?
Sep 7, 2022, 12:14 PM
K
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
Y
it's
/[slug]
Sep 7, 2022, 12:20 PM
K
That‘s it? Nothing else? Just
/pages/[slug].js?
Sep 7, 2022, 12:21 PM
Y
yes exactly !
Sep 7, 2022, 12:22 PM
K
But then where do you expect
tagsto come from exactly?
Sep 7, 2022, 12:23 PM
K
Because
context.paramscontains your routing parameters. In that route of yours, there is only
slug. Not
tags.
Sep 7, 2022, 12:23 PM
Y
oh
Sep 7, 2022, 12:23 PM
Y
so how can I pass the
tagsas well ?
Sep 7, 2022, 12:24 PM
K
/[slug]/[tags].jsor something.
Sep 7, 2022, 12:24 PM
Y
No, my idea is to get recommendation chapters in the same page !
Sep 7, 2022, 12:24 PM
Y
so I have to fetch the exact same
tagsof all the document
Sep 7, 2022, 12:25 PM
K
getStaticPathsis there to generate all the possible paths for the current route. Typically, if you have a route with a dynamic
slugparameter, then your
getStaticPathsfunction needs to return all the existing slugs so that Next.js can statically build all your pages.
Sep 7, 2022, 12:26 PM
Y
I understand
Sep 7, 2022, 12:27 PM
Y
Do you have any idea how possibly I can manipulate the code to receive
tagsvalues as well
Sep 7, 2022, 12:28 PM
K
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
K
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
Y
by collections you mean the multiple documents of
chapters?
Sep 7, 2022, 12:31 PM
K
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
Y
the same problem !
Sep 7, 2022, 12:36 PM
K
Check the data from the query. If
tagsis null, it’s because it’s missing from your document maybe.
Sep 7, 2022, 12:38 PM
Y
I tested the query it works perfectly
Sep 7, 2022, 12:42 PM
Y
are you sure about this part ?
Sep 7, 2022, 12:43 PM
Y
{ params: { slug: chapter.slug } }, { params: { slug: chapter.tags } },
Sep 7, 2022, 12:43 PM
K
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
Y
when I map the
chaptersquery :
chapters.map((s) =>{ console.log(s.slug, s.tags) })
Sep 7, 2022, 12:49 PM
Y
I get this
Sep 7, 2022, 12:49 PM
Y
slug , tags values
Sep 7, 2022, 12:49 PM
Y
each
slugcorresponded to a
tagsvalue
Sep 7, 2022, 12:50 PM
Y
and that's why I want to send the
tagsvalue of each
slugto the getStaticProps so I can fetch the documents
Sep 7, 2022, 12:51 PM
K
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
Y
right
Sep 7, 2022, 12:51 PM
Y
it slug
Sep 7, 2022, 12:52 PM
K
Wait.
Sep 7, 2022, 12:52 PM
K
Only via the slug?
Sep 7, 2022, 12:52 PM
Y
yes only via the slug
Sep 7, 2022, 12:52 PM
Y
is it possible ?
Sep 7, 2022, 12:52 PM
K
Sure, but then I don’t get why you need tags at all.
Sep 7, 2022, 12:52 PM
K
In
getStaticProps, you receive the slug and you can use it to query the right document.
Sep 7, 2022, 12:53 PM
K
Why do you need tags?
Sep 7, 2022, 12:53 PM
Y
I have to use the tags so I can fetch a recommendation chapters in the UI
Sep 7, 2022, 12:53 PM
Y
something lie : You might also like
Sep 7, 2022, 12:53 PM
K
Are
tagsshared across chapters?
Sep 7, 2022, 12:53 PM
Y
yes ofc
Sep 7, 2022, 12:53 PM
K
AAaaaaaah.
Sep 7, 2022, 12:53 PM
K
Okay I finally get it.
Sep 7, 2022, 12:53 PM
K
Alright.
Sep 7, 2022, 12:53 PM
Y
all chapters have tags
Sep 7, 2022, 12:53 PM
K
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
K
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
Y
AAAA
Sep 7, 2022, 12:57 PM
Y
works magically !!
Sep 7, 2022, 12:57 PM
Y
Thank you so much
Sep 7, 2022, 12:57 PM
K
Amazing. 😄
Sep 7, 2022, 12:58 PM
K
Took a while but we got there.
Sep 7, 2022, 12:58 PM
Y
Yeah, I have to be more specific the next time
Sep 7, 2022, 1:00 PM
K
I'm glad we found a way. 🙂
Sep 7, 2022, 1:01 PM
Y
By the way how can I filter the documents to fetch all the same
tagsbut not the current one !
Sep 7, 2022, 1:13 PM
K
*[ _type == "chapters" && tags == $tags && slug.current != $slug ]
Sep 7, 2022, 1:17 PM
Y
ah yeah I was trying
!==instead
Sep 7, 2022, 1:17 PM
Y
thanks.
Sep 7, 2022, 1:17 PM
S
hello
🙏And wonderful
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.