fix(nuxt): ensure middleware is processed when returning `true` (#22905)

This commit is contained in:
Alex Liu 2023-08-31 17:19:26 +08:00 committed by GitHub
parent 1f98f34c8e
commit 2d46971162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 2 deletions

View File

@ -58,7 +58,7 @@ function getRouteFromPath (fullPath: string | Partial<Route>) {
}
}
type RouteGuardReturn = void | Error | string | false
type RouteGuardReturn = void | Error | string | boolean
interface RouteGuard {
(to: Route, from: Route): RouteGuardReturn | Promise<RouteGuardReturn>
@ -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 }
}
}

View File

@ -179,6 +179,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
}
}
if (result === true) { continue }
if (result || result === false) {
return result
}

View File

@ -4,4 +4,5 @@ export default defineNuxtRouteMiddleware((to) => {
statusCode: 401
})
}
return true
})