Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The browser gives up after Next.js middleware redirects the same page again and again

ErrorERR_TOO_MANY_REDIRECTS

Short answer

A redirect loop means your middleware (proxy.ts in Next.js 16) redirects a request that it then receives again. Follow the Location headers with curl to see which path repeats, then stop the middleware from running on that path: add a condition for the target page, narrow the matcher, or make NextAuth's pages setting identical in both places.

  • Applies to Next.js 15, Next.js 16, next-auth 4, Chrome
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Follow the redirects from the command line and read the Location header of each hop. The path that keeps coming back is the one your middleware must leave alone.

    curl -sIL --max-redirs 5 http://localhost:3000/dashboard | grep -iE '^(HTTP|location)'
  2. 2Match what you see, then jump to its fix

    1. 1Every hop answers 307 and the Location header points at /login, then /login againThe middleware also runs on the page it redirects toFix
    2. 2Requests for /_next/static/... or images in public/ answer with a redirect to the login pageThere is no matcher, so static files get redirected as wellFix
    3. 3The project uses next-auth/middleware and the login path in the middleware differs from the one in the [...nextauth] route, or one of them sets no pages optionNextAuth withAuth and the NextAuth route disagree about the sign-in pageFix
    4. 4The loop shows in your normal browser window but not in a private window, even after the code is fixedA stale cookie for this host keeps the loop aliveFix

What does the error look like?

  • ERR_TOO_MANY_REDIRECTS
  • This webpage has a redirect loop

Cause 1 of 4

The middleware also runs on the page it redirects to

1Check

Next.js calls the middleware for every route in the project. If your code sends a visitor without a session to /login, the request for /login passes through the same code, finds no session again and redirects again.

grep -n "redirect" middleware.ts proxy.ts 2>/dev/null
grep -n -A4 "matcher" middleware.ts proxy.ts 2>/dev/null

2Fix

Let the target page through before the redirect rule, or limit the matcher to the paths that really need protection. Handle the reverse case too, so a signed-in visitor is not bounced between two pages.

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  const { pathname } = request.nextUrl
  const hasSession = request.cookies.has('session')

  if (!hasSession && pathname !== '/login') {
    return NextResponse.redirect(new URL('/login', request.url))
  }
  if (hasSession && pathname === '/login') {
    return NextResponse.redirect(new URL('/dashboard', request.url))
  }
  return NextResponse.next()
}

export const config = {
  matcher: ['/dashboard/:path*', '/login'],
}

3Verify

The same curl command now shows one 307 to /login followed by a 200, instead of 307 responses that repeat.

curl -sIL --max-redirs 5 http://localhost:3000/dashboard | grep -iE '^(HTTP|location)'

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 4

There is no matcher, so static files get redirected as well

1Check

Without a matcher the middleware runs on every request, including files under _next/static, image optimisation and the public folder. An auth redirect then blocks the CSS and JavaScript that the login page itself needs.

grep -c "matcher" middleware.ts proxy.ts 2>/dev/null

2Fix

Export a config with a negative matcher that skips the internal and static paths.

export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}

3Verify

Open the network panel on the login page; the files under /_next/static return 200, not 307.

curl -sI http://localhost:3000/favicon.ico | head -n 1

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 4

NextAuth withAuth and the NextAuth route disagree about the sign-in page

1Check

The NextAuth documentation says that pages.signIn and pages.error set in the middleware must match the pages configuration of your [...nextauth] route. If they differ, the middleware can redirect back to itself.

grep -rn -B1 -A3 "pages" middleware.ts proxy.ts 2>/dev/null
grep -rn -A3 "pages" app/api/auth pages/api/auth 2>/dev/null

2Fix

Put the same pages object in both places, and export a matcher that lists only the protected paths. A matcher keeps every other page, including the login page, outside the protection.

// middleware.ts
import { withAuth } from 'next-auth/middleware'

export default withAuth({
  pages: { signIn: '/login' },
})

export const config = { matcher: ['/dashboard/:path*'] }

// authOptions used by the [...nextauth] route
pages: { signIn: '/login' },

3Verify

Open /dashboard signed out: you land on /login once, and the login form renders.

curl -sIL --max-redirs 5 http://localhost:3000/dashboard | grep -iE '^(HTTP|location)'

Confirmed by next-auth.js.org

Did this fix it?

Cause 4 of 4

A stale cookie for this host keeps the loop alive

1Check

Chrome's own help names faulty cookies as a reason for this error.

# browser: open the same URL in a private window and compare

2Fix

Delete the cookies of this one site in the browser, then reload.

# browser: Chrome settings, delete the cookies for localhost only

3Verify

The page loads in the normal window the same way it does in the private one.

# browser: reload after clearing the site's cookies

Confirmed by support.google.com

Did this fix it?

How do I stop it coming back?

  • Whenever middleware redirects to a path, write down that the same code will run again for that path, and give it an explicit way out.
  • Always export a matcher; keep the login, API and static paths out of any auth redirect.
  • Test the signed-out and signed-in cases by hand after every change to the middleware, with curl or a private window.
  • Keep the NextAuth pages setting in one shared constant that both the route and the middleware import.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Next.js middleware (proxy) does not run for my routes

Redirects and rewrites from middleware do nothing

Next.js runs middleware only when the file sits at the project root (or in src) beside app or pages, exports a function with the right name, and the matcher covers the request path.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.