mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(nuxt): don't short circuit middleware after validate function (#9180)
This commit is contained in:
parent
ba940719fe
commit
f4ba7ec82d
@ -1,12 +1,11 @@
|
|||||||
import { createError, defineNuxtRouteMiddleware } from '#app'
|
import { defineNuxtRouteMiddleware } from '#app'
|
||||||
|
|
||||||
export default defineNuxtRouteMiddleware(async (to) => {
|
export default defineNuxtRouteMiddleware(async (to) => {
|
||||||
if (!to.meta?.validate) { return }
|
if (!to.meta?.validate) { return }
|
||||||
|
|
||||||
const result = await Promise.resolve(to.meta.validate(to))
|
const result = await Promise.resolve(to.meta.validate(to))
|
||||||
if (typeof result === 'boolean') {
|
if (result === true) {
|
||||||
return result
|
return
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
return createError(result)
|
|
||||||
})
|
})
|
||||||
|
@ -90,6 +90,9 @@ describe('pages', () => {
|
|||||||
expect(html).toContain('[...slug].vue')
|
expect(html).toContain('[...slug].vue')
|
||||||
expect(html).toContain('404 at not-found')
|
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')
|
await expectNoClientErrors('/not-found')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
2
test/fixtures/basic/pages/[...slug].vue
vendored
2
test/fixtures/basic/pages/[...slug].vue
vendored
@ -2,11 +2,13 @@
|
|||||||
<div>
|
<div>
|
||||||
<div>[...slug].vue</div>
|
<div>[...slug].vue</div>
|
||||||
<div>404 at {{ $route.params.slug[0] }}</div>
|
<div>404 at {{ $route.params.slug[0] }}</div>
|
||||||
|
<div>Middleware ran: {{ !!($route.meta.override as any)?.includes('extended middleware') }}</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
|
middleware: ['override'],
|
||||||
validate: to => to.path !== '/forbidden'
|
validate: to => to.path !== '/forbidden'
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user