# 错误：操作被中止（边缘运行时）/ 获取失败：已中止

- **ID:** `nextjs/edge-runtime-fetch-aborted`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

边缘运行时（中间件或边缘函数）中的 fetch 请求超过了 30 秒超时或由于资源限制被中止，导致请求提前终止。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 14.0.0 | active | — | — |
| Next.js 14.1.0 | active | — | — |
| Next.js 14.2.0 | active | — | — |
| Vercel Edge Runtime 2024 | active | — | — |

## 解决方案

1. ```
   将 fetch 逻辑移到运行在 Node.js 上的服务器组件或 API 路由中，这些组件没有 30 秒超时限制。示例：在 app/api/proxy/route.ts 中创建一个执行 fetch 的 API 路由，然后从边缘中间件调用它。
   ```
2. ```
   优化 fetch 目标以更快响应（例如，添加缓存头、减少负载大小或使用 CDN）。同时确保 fetch 使用合理的超时，如 { signal: AbortSignal.timeout(25000) }，以便在硬限制之前优雅地失败。
   ```
3. ```
   如果 fetch 在中间件中，在 fetch 选项中使用 `next: { revalidate: 60 }` 来缓存响应并减少重复调用。
   ```

## 无效尝试

- **** — The Edge Runtime has a hard 30-second timeout per request that cannot be overridden by client-side timeout options; the platform enforces this limit. (90% 失败率)
- **** — XMLHttpRequest is not available in the Edge Runtime; only fetch and a subset of Web APIs are supported. (100% 失败率)
- **** — The underlying timeout or resource limit will still apply on each retry, and infinite retries can exhaust memory or cause cascading failures. (70% 失败率)
