nextjs config_error ai_generated true

错误:不允许使用布局段配置 'X'。请改用 generateMetadata 或 generateStaticParams。

Error: Layout segment config 'X' is not allowed. Use generateMetadata or generateStaticParams instead.

ID: nextjs/layout-segment-config-export

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2023-10-15首次发现

版本兼容性

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

根因分析

在 Next.js app 目录中,布局段配置(如 'layout' 或 'template')不能直接导出;必须替换为正确的数据获取或元数据 API。

English

In Next.js app directory, layout segment configs like 'layout' or 'template' cannot be exported directly; they must be replaced with the correct data-fetching or metadata APIs.

generic

官方文档

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

解决方案

  1. 将布局段配置替换为 generateMetadata。例如,如果你导出了 'metadata',改为:
    
    export async function generateMetadata({ params }) {
      const data = await fetchData(params.slug);
      return { title: data.title };
    }
  2. 如果配置用于静态生成,使用 generateStaticParams:
    
    export async function generateStaticParams() {
      const posts = await fetchPosts();
      return posts.map(post => ({ slug: post.slug }));
    }
  3. 对于动态段,确保布局有 default.tsx 或 loading.tsx 来处理回退状态。

无效尝试

常见但无效的做法:

  1. 70% 失败

    The layout still needs to define metadata or static params; removing the export breaks the page functionality.

  2. 90% 失败

    Next.js only recognizes specific exports; arbitrary names are ignored and cause build errors.

  3. 80% 失败

    Client components cannot use metadata or static params; this creates a different error.