{
  "id": "nextjs/middleware-redirect-loop-internal",
  "signature": "Error: Maximum call stack size exceeded / redirect loop detected in middleware for path '/login'",
  "signature_zh": "错误：超出最大调用堆栈大小 / 在路径 '/login' 的中间件中检测到重定向循环",
  "regex": "Maximum call stack size exceeded|redirect loop detected in middleware",
  "domain": "nextjs",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Middleware redirects a request to the same path (e.g., redirecting /login to /login) or to a path that triggers another redirect back, creating an infinite loop that exhausts the call stack.",
  "root_cause_type": "generic",
  "root_cause_zh": "中间件将请求重定向到相同路径（例如，将 /login 重定向到 /login）或重定向到另一个触发回环的路径，创建无限循环，耗尽调用堆栈。",
  "versions": [
    {
      "version": "Next.js 13.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Next.js 14.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Next.js 14.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The check does not prevent the redirect; it still redirects to the same path, causing an infinite loop.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the user is already on /login, the redirect will point back to /login, creating a loop.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "While this prevents infinite loops, it does not fix the root cause; the middleware still performs unnecessary redirects, and the user may end up on an unintended page.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add a condition to skip the redirect if the user is already on the target path. Example: In middleware.ts, check `if (request.nextUrl.pathname === '/login') { return NextResponse.next(); }` before the redirect logic.",
      "success_rate": 0.95,
      "how": "Add a condition to skip the redirect if the user is already on the target path. Example: In middleware.ts, check `if (request.nextUrl.pathname === '/login') { return NextResponse.next(); }` before the redirect logic.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure the redirect target is a different path. For example, if protecting /dashboard, redirect unauthenticated users to /login, but also ensure /login does not redirect back to /dashboard unless authenticated.",
      "success_rate": 0.9,
      "how": "Ensure the redirect target is a different path. For example, if protecting /dashboard, redirect unauthenticated users to /login, but also ensure /login does not redirect back to /dashboard unless authenticated.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `return NextResponse.redirect(new URL('/login', request.url))` only after verifying the user is not authenticated and the current path is not /login. Example: `if (!isAuthenticated && request.nextUrl.pathname !== '/login') { return NextResponse.redirect(...) }`.",
      "success_rate": 0.95,
      "how": "Use `return NextResponse.redirect(new URL('/login', request.url))` only after verifying the user is not authenticated and the current path is not /login. Example: `if (!isAuthenticated && request.nextUrl.pathname !== '/login') { return NextResponse.redirect(...) }`.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "添加条件，如果用户已经在目标路径上，则跳过重定向。示例：在 middleware.ts 中，在重定向逻辑之前检查 `if (request.nextUrl.pathname === '/login') { return NextResponse.next(); }`。",
    "确保重定向目标是不同的路径。例如，保护 /dashboard 时，将未认证用户重定向到 /login，同时确保 /login 不会在未认证时重定向回 /dashboard。",
    "仅在验证用户未认证且当前路径不是 /login 后使用 `return NextResponse.redirect(new URL('/login', request.url))`。示例：`if (!isAuthenticated && request.nextUrl.pathname !== '/login') { return NextResponse.redirect(...) }`。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://nextjs.org/docs/app/building-your-application/routing/middleware#avoiding-redirect-loops",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-06-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}