From 403603a934220a00c4d6c013498b4fb8b9aa8832 Mon Sep 17 00:00:00 2001 From: julien huang Date: Fri, 18 Nov 2022 23:18:50 +0100 Subject: [PATCH 1/4] fix(nuxt): allow running global middleware and comp middleware before validate --- packages/nuxt/src/pages/module.ts | 5 ----- packages/nuxt/src/pages/runtime/router.ts | 4 +++- packages/nuxt/src/pages/runtime/validate.ts | 13 ++++++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index d9ec94af32c..904ff897167 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -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 diff --git a/packages/nuxt/src/pages/runtime/router.ts b/packages/nuxt/src/pages/runtime/router.ts index da84dcb5f16..05da430aa34 100644 --- a/packages/nuxt/src/pages/runtime/router.ts +++ b/packages/nuxt/src/pages/runtime/router.ts @@ -10,6 +10,7 @@ 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' @@ -17,7 +18,6 @@ import _routes from '#build/routes' 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 @@ -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 diff --git a/packages/nuxt/src/pages/runtime/validate.ts b/packages/nuxt/src/pages/runtime/validate.ts index 14d5a8aeddc..018c1c100c8 100644 --- a/packages/nuxt/src/pages/runtime/validate.ts +++ b/packages/nuxt/src/pages/runtime/validate.ts @@ -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 +} From 76fff4627d06b6e110c1662c42e4962b2366c601 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 14 Jan 2023 00:07:50 +0000 Subject: [PATCH 2/4] test: add test for mw compatibility --- test/basic.test.ts | 3 +++ test/fixtures/basic/pages/[...slug].vue | 2 ++ 2 files changed, 5 insertions(+) diff --git a/test/basic.test.ts b/test/basic.test.ts index 81ba31c9967..8752d1fc437 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -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') }) diff --git a/test/fixtures/basic/pages/[...slug].vue b/test/fixtures/basic/pages/[...slug].vue index a805ed7aea9..bafe37c5040 100644 --- a/test/fixtures/basic/pages/[...slug].vue +++ b/test/fixtures/basic/pages/[...slug].vue @@ -2,11 +2,13 @@
[...slug].vue
404 at {{ $route.params.slug[0] }}
+
Middleware ran: {{ !!$route.meta.override?.includes('extended middleware') }}
From d009c7356d6f821b4116a679a46f8583f179fc56 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 14 Jan 2023 00:11:06 +0000 Subject: [PATCH 3/4] refactor: move validate middleware back to beginning --- packages/nuxt/src/pages/module.ts | 5 +++++ packages/nuxt/src/pages/runtime/router.ts | 4 +--- packages/nuxt/src/pages/runtime/validate.ts | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 0c4f4beea69..32b38b28579 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -84,6 +84,11 @@ 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 diff --git a/packages/nuxt/src/pages/runtime/router.ts b/packages/nuxt/src/pages/runtime/router.ts index e4df1ee21ff..f01ce3b0d2b 100644 --- a/packages/nuxt/src/pages/runtime/router.ts +++ b/packages/nuxt/src/pages/runtime/router.ts @@ -12,7 +12,6 @@ import { import { createError } from 'h3' import { withoutBase, isEqual } from 'ufo' import type 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' @@ -20,6 +19,7 @@ import _routes from '#build/routes' 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 @@ -138,8 +138,6 @@ 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 diff --git a/packages/nuxt/src/pages/runtime/validate.ts b/packages/nuxt/src/pages/runtime/validate.ts index 018c1c100c8..e66421d9a3e 100644 --- a/packages/nuxt/src/pages/runtime/validate.ts +++ b/packages/nuxt/src/pages/runtime/validate.ts @@ -1,6 +1,6 @@ -import { RouteLocation } from 'vue-router' +import { defineNuxtRouteMiddleware } from '#app' -export default async (to: RouteLocation) => { +export default defineNuxtRouteMiddleware(async (to) => { if (!to.meta?.validate) { return } const result = await Promise.resolve(to.meta.validate(to)) @@ -8,4 +8,4 @@ export default async (to: RouteLocation) => { return } return result -} +}) From 396540f6759598d702573f90a3167a4f78fe9491 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 14 Jan 2023 00:15:48 +0000 Subject: [PATCH 4/4] test: fix type --- test/fixtures/basic/pages/[...slug].vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/basic/pages/[...slug].vue b/test/fixtures/basic/pages/[...slug].vue index bafe37c5040..8ed645e31ad 100644 --- a/test/fixtures/basic/pages/[...slug].vue +++ b/test/fixtures/basic/pages/[...slug].vue @@ -2,7 +2,7 @@
[...slug].vue
404 at {{ $route.params.slug[0] }}
-
Middleware ran: {{ !!$route.meta.override?.includes('extended middleware') }}
+
Middleware ran: {{ !!($route.meta.override as any)?.includes('extended middleware') }}