diff --git a/packages/nuxt/src/app/plugins/router.ts b/packages/nuxt/src/app/plugins/router.ts index 06c5764c31..4060ca79fa 100644 --- a/packages/nuxt/src/app/plugins/router.ts +++ b/packages/nuxt/src/app/plugins/router.ts @@ -58,7 +58,7 @@ function getRouteFromPath (fullPath: string | Partial) { } } -type RouteGuardReturn = void | Error | string | false +type RouteGuardReturn = void | Error | string | boolean interface RouteGuard { (to: Route, from: Route): RouteGuardReturn | Promise @@ -130,7 +130,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({ // Cancel navigation if (result === false || result instanceof Error) { return } // Redirect - if (result) { return handleNavigation(result, true) } + if (typeof result === 'string' && result.length) { return handleNavigation(result, true) } } for (const handler of hooks['resolve:before']) { @@ -250,6 +250,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({ return nuxtApp.runWithContext(() => showError(error)) } } + if (result === true) { continue } if (result || result === false) { return result } } } diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index c535db1b6e..b8de9504b9 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -179,6 +179,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ } } + if (result === true) { continue } if (result || result === false) { return result } diff --git a/test/fixtures/basic/middleware/abort.global.ts b/test/fixtures/basic/middleware/abort.global.ts index efc4f51753..d278883f76 100644 --- a/test/fixtures/basic/middleware/abort.global.ts +++ b/test/fixtures/basic/middleware/abort.global.ts @@ -4,4 +4,5 @@ export default defineNuxtRouteMiddleware((to) => { statusCode: 401 }) } + return true })