Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): don't short circuit middleware after validate function (#9180
Browse files Browse the repository at this point in the history
)
  • Loading branch information
huang-julien committed Jan 14, 2023
1 parent ba94071 commit f4ba7ec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/nuxt/src/pages/runtime/validate.ts
Original file line number Diff line number Diff line change
@@ -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
})
3 changes: 3 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/basic/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit f4ba7ec

Please sign in to comment.