nextjs
runtime_error
ai_generated
true
Error: Maximum call stack size exceeded / redirect loop detected in middleware for path '/login'
ID: nextjs/middleware-redirect-loop-internal
92%Fix Rate
87%Confidence
1Evidence
2023-06-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Next.js 13.0.0 | active | — | — | — |
| Next.js 14.0.0 | active | — | — | — |
| Next.js 14.2.0 | active | — | — | — |
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.
generic中文
中间件将请求重定向到相同路径(例如,将 /login 重定向到 /login)或重定向到另一个触发回环的路径,创建无限循环,耗尽调用堆栈。
Official Documentation
https://nextjs.org/docs/app/building-your-application/routing/middleware#avoiding-redirect-loopsWorkarounds
-
95% success 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.
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. -
90% success 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.
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.
-
95% success 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(...) }`.
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(...) }`.
中文步骤
添加条件,如果用户已经在目标路径上,则跳过重定向。示例:在 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(...) }`。
Dead Ends
Common approaches that don't work:
-
100% fail
The check does not prevent the redirect; it still redirects to the same path, causing an infinite loop.
-
90% fail
If the user is already on /login, the redirect will point back to /login, creating a loop.
-
70% fail
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.