[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-vue-router-infinite-redirect-loop-guard":3},{"content":4,"lastModified":5},"\u003Cp>Staging deploy last Wednesday, the kind of route guard everyone writes. No token, go to \u003Ccode>\u002Flogin\u003C\u002Fcode>. 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.\u003C\u002Fp>\n\n\u003Cp>Dev never did that. Locally the same guard printed a warning and gave up.\u003C\u002Fp>\n\n\u003Ch2>The warning that only exists in development\u003C\u002Fh2>\n\n\u003Cp>I reproduced it first in plain Node, a memory-history router with nothing rendered. The guard sends you to \u003Ccode>\u002Flogin\u003C\u002Fcode> every time, the copy-paste bug in its purest form:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">router.beforeEach(() => {\n  return { name: 'login' }\n})\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Development build, it gives up after 31 redirects and rejects the push:\u003C\u002Fp>\n\n\u003Cblockquote>Detected a possibly infinite redirection in a navigation guard when going from \"\u002F\" to \"\u002Flogin\". Aborting to avoid a Stack Overflow.\u003C\u002Fblockquote>\n\n\u003Cp>The rejection is a plain \u003Ccode>Error\u003C\u002Fcode> reading \u003Ccode>Infinite redirect in navigation guard\u003C\u002Fcode>, not a NavigationFailure, so my \u003Ccode>isNavigationFailure(error)\u003C\u002Fcode> branch never saw it and it hit Sentry as a crash.\u003C\u002Fp>\n\n\u003Cp>The production build of the same router did not give up. Five seconds of wall clock:\u003C\u002Fp>\n\n\u003Ctable>\n  \u003Cthead>\n    \u003Ctr>\u003Cth>\u003C\u002Fth>\u003Cth>Development\u003C\u002Fth>\u003Cth>Production\u003C\u002Fth>\u003C\u002Ftr>\n  \u003C\u002Fthead>\n  \u003Ctbody>\n    \u003Ctr>\u003Ctd>Guard runs\u003C\u002Ftd>\u003Ctd>32, then abort\u003C\u002Ftd>\u003Ctd>1,175,985 and counting\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>1ms timer callbacks fired\u003C\u002Ftd>\u003Ctd>16\u003C\u002Ftd>\u003Ctd>0\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>push()\u003C\u002Fcode> promise\u003C\u002Ftd>\u003Ctd>rejected\u003C\u002Ftd>\u003Ctd>still pending\u003C\u002Ftd>\u003C\u002Ftr>\n    \u003Ctr>\u003Ctd>\u003Ccode>currentRoute\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>\u003Ccode>\u002F\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>\u003Ccode>\u002F\u003C\u002Fcode>\u003C\u002Ftd>\u003C\u002Ftr>\n  \u003C\u002Ftbody>\n\u003C\u002Ftable>\n\n\u003Cp>vue-router 4.6.5, Node 22, same script. The reason is one line in \u003Ccode>pushWithRedirect\u003C\u002Fcode>: the abort sits inside a \u003Ccode>process.env.NODE_ENV !== 'production'\u003C\u002Fcode> branch. Your bundler inlines that as \u003Ccode>false\u003C\u002Fcode> and throws the detector away, so the safety net I had relied on for two years exists only on my laptop.\u003C\u002Fp>\n\n\u003Ch2>A hang, not a stack overflow\u003C\u002Fh2>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>Where a real stack overflow does live: route-level redirects. A record with \u003Ccode>redirect: '\u002Fb'\u003C\u002Fcode> pointing at one with \u003Ccode>redirect: '\u002Fa'\u003C\u002Fcode> blew up before a single guard ran with \u003Ccode>RangeError: Maximum call stack size exceeded\u003C\u002Fcode>, thrown synchronously out of \u003Ccode>router.push\u003C\u002Fcode>. Same class of bug, different failure mode, and the warning only describes this one.\u003C\u002Fp>\n\n\u003Ch2>Why dev misses the login ping-pong too\u003C\u002Fh2>\n\n\u003Cp>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:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">router.beforeEach((to) => {\n  if (to.path === '\u002Fdashboard') return { name: 'login' }\n  if (to.path === '\u002Flogin') return { path: '\u002Fdashboard' }\n})\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>156,195 guard runs in 1.2 seconds, in the development build. No warning, no abort, nothing in the console.\u003C\u002Fp>\n\n\u003Cp>My real guard was worse: it carried the return path in \u003Ccode>query.redirect\u003C\u002Fcode>. Every hop is then a different location, the equality check can never match, and the URL compounds:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">return { name: 'login', query: { redirect: to.fullPath } }\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Four hops in, the target is \u003Ccode>\u002Flogin?redirect=\u002Flogin?redirect=\u002Flogin?redirect=\u002Fdashboard\u003C\u002Fcode>. 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.\u003C\u002Fp>\n\n\u003Cfigure>\n  \u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F5869898\u002Fpexels-photo-5869898.jpeg?auto=compress&amp;cs=tinysrgb&amp;fit=crop&amp;w=1200&amp;h=800\" alt=\"Looking down the well of a spiral staircase, the steps curving away below and no landing visible\" loading=\"lazy\" width=\"1200\" height=\"800\">\n  \u003Cfigcaption>You keep taking steps. The floor does not get closer.\u003C\u002Ffigcaption>\n\u003C\u002Ffigure>\n\n\u003Ch2>The guard I ship now\u003C\u002Fh2>\n\n\u003Cp>Two changes, and neither one needs vue-router to notice anything.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-js\">const routes = [\n  { path: '\u002Fdashboard', component: DashboardView, meta: { requiresAuth: true } },\n  { path: '\u002Flogin', name: 'login', component: LoginView, meta: { public: true } },\n]\n\nlet redirects = 0\n\nrouter.beforeEach((to) => {\n  if (to.meta.public) return true\n  if (useAuthStore().token) return true\n  if (++redirects &gt; 3) return false\n  return { name: 'login', query: { redirect: to.fullPath } }\n})\n\nrouter.afterEach(() => { redirects = 0 })\nrouter.onError(() => { redirects = 0 })\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Against the ping-pong it stops on the fourth call. The navigation resolves to a failure of type 4, \u003Ccode>Navigation aborted from \"\u002F\" to \"\u002Fdashboard\" via a navigation guard.\u003C\u002Fcode> Timers keep firing and the user stays on the route they came from. Normal login flow costs two guard runs.\u003C\u002Fp>\n\n\u003Cp>The reset in \u003Ccode>afterEach\u003C\u002Fcode> is the whole trick and it looks pointless. It is not: during a loop nothing lands, so \u003Ccode>afterEach\u003C\u002Fcode> never fires and the count only climbs; every navigation that does land clears it.\u003C\u002Fp>\n\n\u003Cp>Declaring public routes in \u003Ccode>meta\u003C\u002Fcode> instead of comparing against \u003Ccode>\u002Flogin\u003C\u002Fcode> 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.\u003C\u002Fp>\n\n\u003Cp>One trap while you are in there: \u003Ccode>to.redirectedFrom\u003C\u002Fcode> is the original target, not the previous hop. On a chain from \u003Ccode>\u002Fa\u003C\u002Fcode> to \u003Ccode>\u002Fb\u003C\u002Fcode> to \u003Ccode>\u002Fc\u003C\u002Fcode> the guard sees \u003Ccode>\u002Fa\u003C\u002Fcode> as \u003Ccode>redirectedFrom\u003C\u002Fcode> at both hops, so counting with it does not work the way the name suggests.\u003C\u002Fp>\n\n\u003Ch2>Test for the failure, not the warning\u003C\u002Fh2>\n\n\u003Cp>What changed: I stopped assuming a redirect bug announces itself locally. Build once with \u003Ccode>NODE_ENV=production\u003C\u002Fcode> and click through the auth flow, the same discipline that catches \u003Ca href=\"\u002Fposts\u002Fnuxt-runtimeconfig-env-vars-undefined-production\u002F\">config that only breaks in a production build\u003C\u002Fa>. 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.\u003C\u002Fp>\n\n\u003Cp>This guard and the abort pattern are in \u003Ca href=\"\u002Fsnippetark\u002F\">Snippet Ark\u003C\u002Fa>, 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.\u003C\u002Fp>\n","2026-11-27",1796114112903]