Next.js Discord

Discord Forum

Can't see params in generateStaticParams()

Unanswered
Madeiran sardinella posted this in #help-forum
Open in Discord
Madeiran sardinellaOP
I have dynamic route articles/[id] and then articles/[id]/article/[...slug]

I am abale to generate metadata by using generateMetadata and I can see my params in there and I can capture id I need for request,

export async function generateMetadata(
  { params, searchParams }: Props,
  parent: ResolvingMetadata
): Promise<Metadata> {
  // read route params

  const campaign_id = parseInt(params.id);
  const article_id = parseInt(params.slug[0]);

  // fetch data
  const res = await fetch(
    process.env.API_KEY + "/article_route",
    {
      headers: {
        request: JSON.stringify({
          campaign_id,
          article_id,
        }),
      },
    }
  );

  const data: any = await res.json();
  const article: ArticleType = data.message.article_detail;

  // optionally access and extend (rather than replace) parent metadata
  const previousImages = (await parent).openGraph?.images || [];

  return {
    title: article.title,
    description: article.article.substring(0, 150),
    openGraph: {
      type: "website",
      title: article.title,
      description: article.article.substring(0, 150),
      images: [{ url: article.image_url }, ...previousImages],
    },
    twitter: {
      title: article.title,
      description: article.article.substring(0, 150),
      images: [{ url: article.image_url }, ...previousImages],
    },
  };
}

but unfortunately when I do same for generateStaticParams() the params object is empty ?

export async function generateStaticParams({ params, searchParams }: Props) {
  console.log(params.id) // undefined
}


can anyone explain why is it happening, I am stuck ?

29 Replies

but unfortunately when I do same for generateStaticParams() the params object is empty ?

You need to use generateStaticParams in /articles/[id] too
Madeiran sardinellaOP
Hey, I do have in there
does it work?
Madeiran sardinellaOP
i cant see id in params there either
ofc, you need to generate it
Madeiran sardinellaOP
I am generating

export const generateStaticParams = async () => {
  const response = await fetch(
    process.env.NEXT_PUBLIC_API_KEY + "/get_newsletter_list_public"
  );

  const { message }: { message: Newsletter[] } = await response.json();

  return message.map((newsletter) => ({
    id: newsletter.campaign_id.toString(),
  }));
};
what does it say on build log?
Madeiran sardinellaOP
you can see id being returned
the problem comes only when I to the page inside [...slug] it says it can't find id now Id is the parameter that is only avaialable to me in params which I cam actually able to get it from proms inside component, but not inside generateStaticParams
I thougth generateMetadata
Yes, i need to see the build log for /articles and their children
Madeiran sardinellaOP
*props
Okay
This is the error
is it able to help you understand my problem?
no
coz you didnt show me build log
Madeiran sardinellaOP
But these are only logs I see in my console as I can't proceed further
have you tried running npm run build
Madeiran sardinellaOP
but if I don't get params I am unable to run build because I need that for export config
I am unable to generate params for the final article page
Madeiran sardinellaOP
Yes I can do that
here is the array
hey are you there?
@aardani