nextjs config_error ai_generated true

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

ID: nextjs/layout-segment-config-export

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-10-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
[email protected] active
[email protected] active
[email protected] active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 90% success Replace the layout segment config with generateMetadata. For example, if you exported 'metadata', change to: export async function generateMetadata({ params }) { const data = await fetchData(params.slug); return { title: data.title }; }
    Replace the layout segment config with generateMetadata. For example, if you exported 'metadata', change to:
    
    export async function generateMetadata({ params }) {
      const data = await fetchData(params.slug);
      return { title: data.title };
    }
  2. 85% success If the config was for static generation, use generateStaticParams: export async function generateStaticParams() { const posts = await fetchPosts(); return posts.map(post => ({ slug: post.slug })); }
    If the config was for static generation, use generateStaticParams:
    
    export async function generateStaticParams() {
      const posts = await fetchPosts();
      return posts.map(post => ({ slug: post.slug }));
    }
  3. 75% success For dynamic segments, ensure the layout has a default.tsx or loading.tsx to handle fallback states.
    For dynamic segments, ensure the layout has a default.tsx or loading.tsx to handle fallback states.

中文步骤

  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 来处理回退状态。

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 90% fail

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

  3. 80% fail

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