错误:操作被中止(边缘运行时)/ 获取失败:已中止
Error: The operation was aborted. (Edge Runtime) / fetch failed: aborted
ID: nextjs/edge-runtime-fetch-aborted
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Next.js 14.0.0 | active | — | — | — |
| Next.js 14.1.0 | active | — | — | — |
| Next.js 14.2.0 | active | — | — | — |
| Vercel Edge Runtime 2024 | active | — | — | — |
根因分析
边缘运行时(中间件或边缘函数)中的 fetch 请求超过了 30 秒超时或由于资源限制被中止,导致请求提前终止。
English
A fetch request in the Edge Runtime (middleware or edge function) exceeded the 30-second timeout or was aborted due to a resource limit, causing the request to be terminated prematurely.
官方文档
https://nextjs.org/docs/messages/edge-runtime-timeout解决方案
-
将 fetch 逻辑移到运行在 Node.js 上的服务器组件或 API 路由中,这些组件没有 30 秒超时限制。示例:在 app/api/proxy/route.ts 中创建一个执行 fetch 的 API 路由,然后从边缘中间件调用它。
-
优化 fetch 目标以更快响应(例如,添加缓存头、减少负载大小或使用 CDN)。同时确保 fetch 使用合理的超时,如 { signal: AbortSignal.timeout(25000) },以便在硬限制之前优雅地失败。 -
如果 fetch 在中间件中,在 fetch 选项中使用 `next: { revalidate: 60 }` 来缓存响应并减少重复调用。
无效尝试
常见但无效的做法:
-
90% 失败
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.
-
100% 失败
XMLHttpRequest is not available in the Edge Runtime; only fetch and a subset of Web APIs are supported.
-
70% 失败
The underlying timeout or resource limit will still apply on each retry, and infinite retries can exhaust memory or cause cascading failures.