fix(nuxt): don't short circuit middleware after validate function (#9180)

This commit is contained in:
Julien Huang 2023-01-14 01:23:20 +01:00 committed by GitHub
parent ba940719fe
commit f4ba7ec82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 5 deletions

View File

@ -1,12 +1,11 @@
import { createError, defineNuxtRouteMiddleware } from '#app'
import { defineNuxtRouteMiddleware } from '#app'
export default defineNuxtRouteMiddleware(async (to) => {
if (!to.meta?.validate) { return }
const result = await Promise.resolve(to.meta.validate(to))
if (typeof result === 'boolean') {
return result
if (result === true) {
return
}
return createError(result)
return result
})

View File

@ -90,6 +90,9 @@ describe('pages', () => {
expect(html).toContain('[...slug].vue')
expect(html).toContain('404 at not-found')
// Middleware still runs after validation: https://github.com/nuxt/framework/issues/9701
expect(html).toContain('Middleware ran: true')
await expectNoClientErrors('/not-found')
})

View File

@ -2,11 +2,13 @@
<div>
<div>[...slug].vue</div>
<div>404 at {{ $route.params.slug[0] }}</div>
<div>Middleware ran: {{ !!($route.meta.override as any)?.includes('extended middleware') }}</div>
</div>
</template>
<script setup lang="ts">
definePageMeta({
middleware: ['override'],
validate: to => to.path !== '/forbidden'
})
</script>