•4 min read

Vue Router Infinite Redirect Loop: It Only Stops in Dev

Staging deploy last Wednesday, the kind of route guard everyone writes. No token, go to /login. I clicked a protected link, the loading bar appeared, and then nothing. No route change, no error, no console line. The tab sat at full CPU until Chrome offered to kill the page.

Dev never did that. Locally the same guard printed a warning and gave up.

The warning that only exists in development

I reproduced it first in plain Node, a memory-history router with nothing rendered. The guard sends you to /login every time, the copy-paste bug in its purest form:

router.beforeEach(() => {
  return { name: 'login' }
})

Development build, it gives up after 31 redirects and rejects the push:

Detected a possibly infinite redirection in a navigation guard when going from "/" to "/login". Aborting to avoid a Stack Overflow.

The rejection is a plain Error reading Infinite redirect in navigation guard, not a NavigationFailure, so my isNavigationFailure(error) branch never saw it and it hit Sentry as a crash.

The production build of the same router did not give up. Five seconds of wall clock:

DevelopmentProduction
Guard runs32, then abort1,175,985 and counting
1ms timer callbacks fired160
push() promiserejectedstill pending
currentRoute//

vue-router 4.6.5, Node 22, same script. The reason is one line in pushWithRedirect: the abort sits inside a process.env.NODE_ENV !== 'production' branch. Your bundler inlines that as false and throws the detector away, so the safety net I had relied on for two years exists only on my laptop.

A hang, not a stack overflow

The warning says it aborts to avoid a stack overflow. For a guard that is wrong. A redirect returned from a guard starts a new promise chain, so the recursion is asynchronous and the call stack stays flat. What piles up is the microtask queue, which never gets to drain.

That is why zero of my one-millisecond interval callbacks ran during five seconds of looping. No timers means nothing that waits on the event loop runs either, and in a browser that includes paint. Slow would at least be progress. This is stopped.

Memory is not what kills it. RSS went from 55MB to 352MB over five seconds and the collector kept up, heap use bouncing between 60 and 165MB. The tab hangs around as long as you leave it, instead of crashing and telling you something.

Where a real stack overflow does live: route-level redirects. A record with redirect: '/b' pointing at one with redirect: '/a' blew up before a single guard ran with RangeError: Maximum call stack size exceeded, thrown synchronously out of router.push. Same class of bug, different failure mode, and the warning only describes this one.

Why dev misses the login ping-pong too

The detector has a second condition: it only counts when the redirect target resolves to the exact location you were already navigating to, and it takes 30 repeats to trip. A guard that always sends you to login gets caught. Two routes pointing at each other do not:

router.beforeEach((to) => {
  if (to.path === '/dashboard') return { name: 'login' }
  if (to.path === '/login') return { path: '/dashboard' }
})

156,195 guard runs in 1.2 seconds, in the development build. No warning, no abort, nothing in the console.

My real guard was worse: it carried the return path in query.redirect. Every hop is then a different location, the equality check can never match, and the URL compounds:

return { name: 'login', query: { redirect: to.fullPath } }

Four hops in, the target is /login?redirect=/login?redirect=/login?redirect=/dashboard. Fifty-eight thousand characters of URL in 1.2 seconds. The loop is not doing anything expensive; the string building is what slows it down.

Looking down the well of a spiral staircase, the steps curving away below and no landing visible
You keep taking steps. The floor does not get closer.

The guard I ship now

Two changes, and neither one needs vue-router to notice anything.

const routes = [
  { path: '/dashboard', component: DashboardView, meta: { requiresAuth: true } },
  { path: '/login', name: 'login', component: LoginView, meta: { public: true } },
]

let redirects = 0

router.beforeEach((to) => {
  if (to.meta.public) return true
  if (useAuthStore().token) return true
  if (++redirects > 3) return false
  return { name: 'login', query: { redirect: to.fullPath } }
})

router.afterEach(() => { redirects = 0 })
router.onError(() => { redirects = 0 })

Against the ping-pong it stops on the fourth call. The navigation resolves to a failure of type 4, Navigation aborted from "/" to "/dashboard" via a navigation guard. Timers keep firing and the user stays on the route they came from. Normal login flow costs two guard runs.

The reset in afterEach is the whole trick and it looks pointless. It is not: during a loop nothing lands, so afterEach never fires and the count only climbs; every navigation that does land clears it.

Declaring public routes in meta instead of comparing against /login is the other half. My app also has a password reset and an email verification screen, and I forgot both the first time; the loop moved one route over and kept going. A flag on the route is a list I can grep; a path comparison buried in a guard is not.

One trap while you are in there: to.redirectedFrom is the original target, not the previous hop. On a chain from /a to /b to /c the guard sees /a as redirectedFrom at both hops, so counting with it does not work the way the name suggests.

Test for the failure, not the warning

What changed: I stopped assuming a redirect bug announces itself locally. Build once with NODE_ENV=production and click through the auth flow, the same discipline that catches config that only breaks in a production build. That single step was the difference between a console warning and a dead tab, and staring at the dev console was never going to show it.

This guard and the abort pattern are in Snippet Ark, mostly so I stop rewriting the counter every time a project needs auth. The version without the budget is in there too, labelled as the one that froze staging for six minutes.