next.js icon indicating copy to clipboard operation
next.js copied to clipboard

generateStaticParams with next export: Build fails when generateStaticParams returns an empty array of params

Open NasserBvB opened this issue 1 year ago • 2 comments

Link to the code that reproduces this issue

https://github.com/NasserBvB/generate-static-params-issue

To Reproduce

While building a Next.js application with output: "export" in next.config.js, I encountered an error regarding the missing generateStaticParams() function, despite it being implemented and returning an empty list. This error specifically affects dynamic pages in the project.

Notably, the build succeeds as expected when generateStaticParams() returns at least one article parameter in the list. The issue only arises when it returns an empty list ([]).

Steps to Reproduce

  1. Create a new Next.js application using [email protected].
  2. Add a dynamic page at articles/[article]/page.js.
  3. Implement generateStaticParams in articles/[article]/page.js and set it to return an empty list ([]).
  4. Configure output: "export" in next.config.js.
  5. Run the build command.

Expected Behavior

The build should succeed without any issues, recognizing that generateStaticParams() is implemented and returning an empty list of article slugs.

Actual Behavior

An error occurs during the build, specifically stating:

Error: Page "/articles/[article]" is missing "generateStaticParams()" so it cannot be used with "output: export" config.

However, when generateStaticParams() returns a list with at least one article slug, the build completes successfully as expected.

Additional Context

  • I’ve confirmed that generateStaticParams() exists in articles/[article]/page.js, and the issue persists only when it returns an empty list.

Current vs. Expected behavior

Expected Behavior

The build should succeed without any issues, recognizing that generateStaticParams() is implemented and returning an empty list of article slugs.

Actual Behavior

An error occurs during the build, specifically stating:

Error: Page "/articles/[article]" is missing "generateStaticParams()" so it cannot be used with "output: export" config.

However, when generateStaticParams() returns a list with at least one article slug, the build completes successfully as expected.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.0.0: Tue Sep 24 23:36:26 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 18.19.0
  npm: 10.2.3
  Yarn: 1.22.22
  pnpm: 9.12.0
Relevant Packages:
  next: 14.2.8 // An outdated version detected (latest is 15.0.1), upgrade is highly recommended!
  eslint-config-next: 14.2.8
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: export

Which area(s) are affected? (Select all that apply)

create-next-app, Output (export/standalone), Script (next/script)

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

No response

NasserBvB avatar Oct 25 '24 14:10 NasserBvB

Hey @NasserBvB , I take a look at the issue. Looks like you will have to return article in the param object since your dynamic path is /article/[article]/page.tsx.

Take a look at here: https://github.com/NasserBvB/generate-static-params-issue/blob/25021349b5e2430bdc72fab2c0764dff50084448/src/app/articles/%5Barticle%5D/page.tsx#L25C1-L27C7

export async function generateStaticParams() {
  const posts = await mockArticles();

  return posts.map((post) => ({
    slug: post.title, // <- slug should be article
  }));
}

Riley1101 avatar Oct 25 '24 17:10 Riley1101

Yes you are right but even though you correct it you will still get the same thing

NasserBvB avatar Oct 25 '24 18:10 NasserBvB

Hey @NasserBvB , I take a look at the issue. Looks like you will have to return article in the param object since your dynamic path is /article/[article]/page.tsx.

Take a look at here: https://github.com/NasserBvB/generate-static-params-issue/blob/25021349b5e2430bdc72fab2c0764dff50084448/src/app/articles/%5Barticle%5D/page.tsx#L25C1-L27C7

export async function generateStaticParams() {
  const posts = await mockArticles();

  return posts.map((post) => ({
    slug: post.title, // <- slug should be article
  }));
}

Hi, the error still appears with this method

samsan1212 avatar Oct 29 '24 07:10 samsan1212