Next.js Discord

Discord Forum

dynamicParams = false; not working. Dynamic segments are still generated on demand.

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I've added the line export const dynamicParams = false; to have any dynamic segments redirect to the 404 page, yet any slug for this page will try to generate the page on demand. For example /jobs/intern should work since it's included in the static list of slugs, but jobs/some-random-slug shouldn't, since it isn't. This causes an internal server error since the data fetching obviously fails. Not sure what I've missed.

Code:
export async function generateStaticParams() {
  const jobs = await fetchJobs();
  const slugs = jobs
    .filter((job) => job.publish && job.slug)
    .map((job) => job.slug);

  return slugs;
}

export default async function Job({ params }) {
  const { slug } = params;

  const job = await fetchJobBySlug(slug);
  const generalContent = await fetchGeneralContent();

  return (
    <Layout generalContent={generalContent}>
      <JobInfo job={job} />
    </Layout>
  );
}

export const dynamicParams = false;
Answered by Ray
export async function generateStaticParams() {
  const jobs = await fetchJobs();
  const slugs = jobs
    .filter((job) => job.publish && job.slug)
    .map((job) => ({ slug: job.slug });

  return slugs;
}
View full answer

6 Replies

export async function generateStaticParams() {
  const jobs = await fetchJobs();
  const slugs = jobs
    .filter((job) => job.publish && job.slug)
    .map((job) => ({ slug: job.slug });

  return slugs;
}
Answer
need to return an array of object
you returning an array of string i guess?
Asiatic LionOP
Ohh, right. How did I miss that. I wrote it like that for my other dynamic pages
Thank you :)