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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ export default defineNuxtModule({
if (app.mainComponent!.includes('@nuxt/ui-templates')) {
app.mainComponent = resolve(runtimeDir, 'app.vue')
}
app.middleware.unshift({
name: 'validate',
path: resolve(runtimeDir, 'validate'),
global: true
})
})

// Prerender all non-dynamic page routes when generating app
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/src/pages/runtime/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
import { createError } from 'h3'
import { withoutBase, isEqual } from 'ufo'
import NuxtPage from './page'
import validate from './validate'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, showError, clearError, navigateTo, useError, useState } from '#app'
// @ts-ignore
import _routes from '#build/routes'
// @ts-ignore
import routerOptions from '#build/router.options'
// @ts-ignore
import { globalMiddleware, namedMiddleware } from '#build/middleware'

declare module '@vue/runtime-core' {
export interface GlobalComponents {
NuxtPage: typeof NuxtPage
Expand Down Expand Up @@ -136,6 +136,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
}
}

middlewareEntries.add(() => validate(to))

for (const entry of middlewareEntries) {
const middleware = typeof entry === 'string' ? nuxtApp._middleware.named[entry] || await namedMiddleware[entry]?.().then((r: any) => r.default || r) : entry

Expand Down
13 changes: 6 additions & 7 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 { RouteLocation } from 'vue-router'

export default defineNuxtRouteMiddleware(async (to) => {
export default async (to: RouteLocation) => {
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
}