# 错误：不变量：headers() 期望有 requestAsyncStorage，但当前不可用。

- **ID:** `nextjs/invariant-requestAsyncStorage-not-available`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

headers() 函数在有效的请求上下文之外被调用，例如在同步函数、实用程序文件或在没有请求的构建期间运行的组件中。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@14.2.0 | active | — | — |
| next@15.0.0-rc.2 | active | — | — |
| next@13.5.6 | active | — | — |

## 解决方案

1. ```
   将 headers() 调用移入可用的请求上下文的异步服务器组件或 API 路由中。
   ```
2. ```
   如果在实用程序函数中需要标头，请从服务器组件将它们作为参数传递。
   ```
3. ```
   如果错误发生在中间件中，请确保 headers() 在中间件函数的顶层调用，而不是在嵌套的回调或条件语句中。
   ```

## 无效尝试

- **** — headers() is a server-only function; it cannot be called in client components even in useEffect. (90% 失败率)
- **** — The function will still throw an invariant error, and the catch block will not provide valid header data. (85% 失败率)
- **** — These functions run at build time without a request context, so headers() is not available. (80% 失败率)
