Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/plugins/router.ts
Expand Up @@ -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>
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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 }
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/pages/runtime/plugins/router.ts
Expand Up @@ -179,6 +179,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
}
}

if (result === true) { continue }
if (result || result === false) {
return result
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/middleware/abort.global.ts
Expand Up @@ -4,4 +4,5 @@ export default defineNuxtRouteMiddleware((to) => {
statusCode: 401
})
}
return true
})