Next.js Discord

Discord Forum

Help with generateStaticParams

Unanswered
Orinoco Crocodile posted this in #help-forum
Open in Discord
Orinoco CrocodileOP
export async function generateStaticParams() {
  return;
}
Can someone explain to me how to use this? I have a dynamic page but cant preload the pages beforehand, only when you load the page itself (requires client side code)

42 Replies

Orinoco CrocodileOP
export async function generateStaticParams() {
  return [];
}
- This doesnt work either, its forcing me to generate with a number which I'd rather not do
Orinoco CrocodileOP
bump
Orinoco CrocodileOP
bump
Orinoco CrocodileOP
bump
Orinoco CrocodileOP
bump
Orinoco CrocodileOP
.
@Orinoco Crocodile .
https://nextjs.org/docs/app/api-reference/functions/generate-static-params


Im not a total expert on Nextjs. I normally just code in it without a reason why, however, never seen people use the function the way you do it. David Gray has a good video on it. From what this does, you map over and return your data so it prebuilds pages on build time. Anyway
Orinoco CrocodileOP
Yeah but pages are generated automatically on load depending if you have an auth token
Balinese
I think the clue is in the name, Static.
If you need Auth to deal with this page. Static params probably are not the solution. Static pages are generated at build time. Do a tutorial or 2 on how to use these and you'll understand why noone has replied here for 6 days
@Orinoco Crocodile Yeah but pages are generated automatically on load depending if you have an auth token
Northeast Congo Lion
your generateStaticParams should return a list of the slugs for that route.

export async function generateStaticParams() {

    const slugsReq = await fetch(...);
    const slugRes = await slugsReq.json();

    return slugRes.map((slug) => ({
        '...slug': slug.path
    }))
}
Northeast Congo Lion
then
wait what
@Orinoco Crocodile And if it's not there at build but is there afterwards
Northeast Congo Lion
usually slug list will come from cms etc
where are you defining pages
Orinoco CrocodileOP
Pages are created dynamically after through the cms?
So at build there me be x page but that page may be deleted in the db
Northeast Congo Lion
then use above
with fallback + isr
genStaticParams will take initial slug list
but if nextjs detects new published page
iirc, it will essentially build it on demand, then store it as part of list
does that make sense?
Orinoco CrocodileOP
And there's no way to do it client side?
Northeast Congo Lion
uhm
i dont follow
Orinoco CrocodileOP
My api requires a auth token which is saved to local storage
Northeast Congo Lion
i would consider moving that from localStorage to a cookie
or env file
@Northeast Congo Lion iirc, it will essentially build it on demand, then store it as part of list
Orinoco CrocodileOP
so appears to not be working - just returns 404 if it cant find the new page
import { SERVER_IP } from "@/_hooks";
import { CreateOrModifyJob } from "@/_pages/createOrModifyJob/createOrModifyJob";

export async function generateStaticParams() {
  const posts = await fetch(`${SERVER_IP}/cms/get-pages?pageName=jobs`).then(
    (res) => res.json()
  );
  return posts.map((post: { jobID: string }) => ({
    edit: post.jobID.toString(),
  }));
}
export default async function Page({ params }: { params: { edit: string } }) {
  return <CreateOrModifyJob id={params.edit} />;
}
Do this @Orinoco Crocodile
Orinoco CrocodileOP
isnt the default true?
Orinoco CrocodileOP
yeah still didnt work
do I need to change something in my nginx conf?
dynamic routes is listed as not supported there
@Arinji
sorry, im not really used to with static exporting