Maaz Zindani

Loading the index…

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

Vue Router rejects a guard that takes next but never calls it

Your error

Error: Invalid navigation guard2 more lines it can show

Fix it fast

Most common cause, 1 of 2

An async guard declares next and returns early

Drop the next parameter and return the outcome from every branch.

Fix
router.beforeEach(async (to) => {
  if (to.name !== 'Login' && !(await isLoggedIn())) {
    return { name: 'Login' }
  }
})
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Vue Router throws "Error: Invalid navigation guard" in development when a guard declares the third next parameter but finishes without calling it: an async guard that returns early, or a plain guard that returns a value. Remove next and return true, false or a route location instead. Applies to Vue Router 4.6 and 5.5; 5.0.3+ also deprecates next.

  • Applies to vue-router 5.5.1, vue-router 4.6.5, Vue 3.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find every guard that still has three parameters. The check only runs when a guard's function length is above 2, so any guard listed here is a suspect; a guard with two parameters cannot raise this error.

Run this first
grep -rnE "(beforeEach|beforeResolve|beforeEnter|beforeRouteEnter|beforeRouteUpdate|beforeRouteLeave)[^)]*next" src

Then find the line you see in this list and open its fix:

  1. 1An async guard declares next and returns earlyThe guard is async (or returns a Promise) and one branch ends with return, await or nothing, without reaching next()
  2. 2A plain guard returns a value but also declares nextThe guard is not async, it has a return false, return '/login' or return true, and next sits unused in its parameter list

What does the error look like?

  • Error: Invalid navigation guard
  • [VUE_ROUTER_R0023] The "next" callback was never called inside of
  • If you are returning a value instead of calling "next", make sure to remove the "next" parameter from your function.

Cause 1 of 2

An async guard declares next and returns early

You seeThe guard is async (or returns a Promise) and one branch ends with return, await or nothing, without reaching next()

Check

Vue Router throws Invalid navigation guard after an async guard's Promise settles if the guard declared next and never called it. The router decides this from the function's parameter count, so an async (to, from, next) guard that resolves on any branch without next() fails even when that branch returned a sensible value.

Check
// fails: next is declared, the early branch never calls it
router.beforeEach(async (to, from, next) => {
  if (!(await isLoggedIn())) return { name: 'Login' }
  next()
})

Fix

Drop the next parameter and return the outcome from every branch. Returning nothing or true lets the navigation continue; returning a location redirects.

Fix
router.beforeEach(async (to) => {
  if (to.name !== 'Login' && !(await isLoggedIn())) {
    return { name: 'Login' }
  }
})

Verify

Navigation reaches the target or the login page, and the console no longer shows the never-called warning.

Verify
grep -rn "async (to, from, next)" src

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A plain guard returns a value but also declares next

You seeThe guard is not async, it has a return false, return '/login' or return true, and next sits unused in its parameter list

Check

For a non-async guard the router accepts a returned value only when the guard has two parameters. With next present, any return other than undefined, without a next() call first, rejects the navigation with Invalid navigation guard. The Vue Router guide says that as long as a guard declares next, it expects next to be called.

Check
// fails: returns a value, next is never used
router.beforeEach((to, from, next) => {
  if (!auth.user) return '/login'
  return true
})

Fix

Use one style per guard. The current guide maps next() to return, next(false) to return false and next('/login') to return '/login'. If you must keep next for now, call it exactly once on every path and return nothing.

Fix
router.beforeEach((to) => {
  if (!auth.user && to.path !== '/login') return '/login'
})

Verify

Run the app in dev mode and trigger the redirect; the route changes without a rejected navigation in the console.

Verify
npm run dev

Confirmed by router.vuejs.org

Did this fix it?

How do I stop it coming back?

  • Write new guards with two parameters only; on Vue Router 5 any call to next already logs a deprecation warning in development.
  • Add the grep from the first step to CI so a three-parameter guard cannot land unnoticed.
  • Exercise the redirect branch of each guard in a dev build: production bundles drop the check, so the same mistake there shows up only as a navigation that never finishes.

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.

Next step

Want this handled for good?

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