# 错误：app/ 目录不支持 getInitialProps。请改用 generateMetadata、generateStaticParams 或服务器组件。

- **ID:** `nextjs/app-dir-getinitialprops-legacy`
- **领域:** nextjs
- **类别:** breaking_change
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

app/ 目录使用新的路由模型，不支持 getInitialProps，这是 Pages Router 的旧功能。从 pages/ 迁移到 app/ 的开发人员错误地使用了 getInitialProps。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 13.x | active | — | — |
| Next.js 14.x | active | — | — |
| Next.js 15.x | active | — | — |

## 解决方案

1. ```
   Replace getInitialProps with server component data fetching: directly fetch data in the component body using async/await. For dynamic metadata, use generateMetadata.
   ```
2. ```
   If the data is needed on every page, use a root layout with a server component that fetches data and passes it via context or props.
   ```

## 无效尝试

- **** — The app router explicitly disallows getInitialProps; it will throw this error at build or runtime. (90% 失败率)
- **** — Even if the component is imported, its getInitialProps will not run; the app router ignores it entirely. (70% 失败率)
- **** — getServerSideProps is also not supported in app/; it must be replaced with server components or fetch with cache options. (60% 失败率)
