Next.js Discord

Discord Forum

generateStaticParams : how to generate them all in only one generator?

Unanswered
Mud-daubers posted this in #help-forum
Open in Discord
Mud-daubersOP
Hello everyone!
I didn't quite understand how "generateStaticParams" works in Next.js.

In my project, I had to duplicate code in each sub-dynamic-path in order to successfully generate all the static params.

I would have appreciated being able to consolidate this code into a single call of the "generateStaticParams" function, but I don't understand how to do it.

Thank you in advance for your responses!


Here is my horrible code:

https://github.com/Tirraa/dashboard_rtm/tree/0b74604f64732bce84bb26b59fde69197b0fcdd5/src/app/%5Blng%5D/%5Bcateg%5D

43 Replies

Mud-daubersOP
To make this a bit more visual, here's a summary of the generated routes:
which code did you duplicate?
the generateStaticParams?
Mud-daubersOP
Yes, I'm duplicating generateStaticParams.

But I'm specifying this call in each sub-dynamic-path.
Paper wasp
He's gay
Have you tried using generateStaticParams to generate multiple dynamic routes?
https://nextjs.org/docs/app/api-reference/functions/generate-static-params#multiple-dynamic-segments-in-a-route
Mud-daubersOP
Yes, but I didn't understand :/
Could you give a look at the code?
It would be more clear, I guess.
i will look into it if i have a demo
Mud-daubersOP
Ofc!
You can clone the repo and use yarn build / yarn run dev
the returned array should be the possible values that the dynamic params can have

[
  { lng: 'fr', categ: 'patch-notes' },
  { lng: 'en', categ: 'patch-notes' },
]
Paper wasp
i'm gay
@Paper wasp i'm gay
Mud-daubersOP
That's not funny at all
@alfon the returned array should be the possible values that the dynamic params can have json [ { lng: 'fr', categ: 'patch-notes' }, { lng: 'en', categ: 'patch-notes' }, ]
Mud-daubersOP
Okay, and what if I need to go deeper than /[lng]/[categ] and stay exhaustive ?
yes
all possible combination
in an array
Mud-daubersOP
I tried but it didn't work, it wasn't exhaustive, and only staticly generated the leafs
you have 2 strategy
eigher you have everything at [lng]
or you have 1 generateStaticParams in each dynamic route
Mud-daubersOP
either you have everything at [lng]
I would like something like this
so in total either have 1 generateStaticParams at [lng]
or
4 generateStaticParams, each at [lng], [categ], [subcateg], [slug]
oh
Mud-daubersOP
but all at [lng]/[categ] instead
4 generateStaticParams, each at [lng], [categ], [subcateg], [slug]
That's what I've done already. But I'm not fancy of it. Somehow, it is code duplication.
so you could have one at [lng] and one at [categ]?
Mud-daubersOP
Yup. I would like to do so.
at /[lng].layout.tsx
and at /[lng]/[categ].page.tsx, maybe?
well have you made sure generateStaticParams at [categ]/layout.tsx returns
{ categ: string, subcateg: string, slug: string }[]
Mud-daubersOP
Yes
The current code generate well everything
I just would like to do some refactor
because I find it horrible to use generateStaticParams at each node until the leafs
also afaik you don't need to use generateStaticParams on the child anymore if you already generate it on the top
Mud-daubersOP
...
Please, understand my question
you want a single generateStaticParms but you suggested having two.. can you restate your question?
Mud-daubersOP
no
okay