nextjs build_error ai_generated true

错误:路径 '/[slug]' 的静态生成失败,因为 'generateStaticParams' 没有为 slug '...' 返回值

Error: Static generation failed for path '/[slug]' because 'generateStaticParams' did not return a value for slug '...'

ID: nextjs/static-generation-failed-for-dynamic-path

其他格式: JSON · Markdown 中文 · English
82%修复率
88%置信度
1证据数
2024-03-20首次发现

版本兼容性

版本状态引入弃用备注
[email protected] active
[email protected] active
[email protected] active

根因分析

generateStaticParams 函数缺失或不完整,因此 Next.js 在构建时无法为动态路由静态生成页面。

English

The generateStaticParams function is missing or incomplete, so Next.js cannot statically generate pages for dynamic routes during build time.

generic

官方文档

https://nextjs.org/docs/app/api-reference/functions/generate-static-params

解决方案

  1. 实现 generateStaticParams 以返回所有可能的 slug 值数组。如果数据源是动态的,请使用 fetch 或数据库查询。
  2. 如果 slug 数量太大,请使用增量静态再生(ISR),在 fetch 或页面配置中添加 revalidate。
  3. 使用动态渲染,设置 `export const dynamic = 'force-dynamic'` 以完全跳过静态生成。

无效尝试

常见但无效的做法:

  1. 80% 失败

    The page will be generated for the dummy slug but still fail for other slugs if they are accessed.

  2. 90% 失败

    This will cause a 404 for any slug not pre-generated, which is not the intended fix.

  3. 95% 失败

    This changes the application structure and may break existing functionality.