From 3cd8a04cfcbb141345afc81be2dc4564e4c46641 Mon Sep 17 00:00:00 2001 From: Saeid Zareie Date: Wed, 22 Jan 2025 16:37:56 +0330 Subject: [PATCH 1/3] fix: extraction of middleware by default in scanPageMeta --- docs/2.guide/3.going-further/1.experimental-features.md | 2 +- packages/nuxt/src/pages/utils.ts | 2 +- packages/schema/src/config/experimental.ts | 2 +- packages/schema/src/types/hooks.ts | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/2.guide/3.going-further/1.experimental-features.md b/docs/2.guide/3.going-further/1.experimental-features.md index 9a4b729e9f..8f6c0630a3 100644 --- a/docs/2.guide/3.going-further/1.experimental-features.md +++ b/docs/2.guide/3.going-further/1.experimental-features.md @@ -332,7 +332,7 @@ globalThis.Buffer = globalThis.Buffer || Buffer ## scanPageMeta -This option allows exposing some route metadata defined in `definePageMeta` at build-time to modules (specifically `alias`, `name`, `path`, `redirect`). +This option allows exposing some route metadata defined in `definePageMeta` at build-time to modules (specifically `alias`, `name`, `path`, `redirect`, `props` and `middleware`). This only works with static or strings/arrays rather than variables or conditional assignment. See [original issue](https://github.com/nuxt/nuxt/issues/24770) for more information and context. diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 5d8c6777ed..e58936bf72 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -202,7 +202,7 @@ export function extractScriptContent (sfc: string) { } const PAGE_META_RE = /definePageMeta\([\s\S]*?\)/ -const defaultExtractionKeys = ['name', 'path', 'props', 'alias', 'redirect'] as const +const defaultExtractionKeys = ['name', 'path', 'props', 'alias', 'redirect', 'middleware'] as const const DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const const pageContentsCache: Record = {} diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index 90c0bc4add..c59730e762 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -295,7 +295,7 @@ export default defineUntypedSchema({ inlineRouteRules: false, /** - * Allow exposing some route metadata defined in `definePageMeta` at build-time to modules (alias, name, path, redirect). + * Allow exposing some route metadata defined in `definePageMeta` at build-time to modules (alias, name, path, redirect, props, middleware). * * This only works with static or strings/arrays rather than variables or conditional assignment. * diff --git a/packages/schema/src/types/hooks.ts b/packages/schema/src/types/hooks.ts index 8ae22441c9..45b80e01d3 100644 --- a/packages/schema/src/types/hooks.ts +++ b/packages/schema/src/types/hooks.ts @@ -34,6 +34,7 @@ export type NuxtPage = { alias?: string[] | string redirect?: RouteLocationRaw children?: NuxtPage[] + middleware?: string[] | string /** * Set the render mode. * From 78887bca23f1225b05e3385d9c29d8f664c4ecce Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:14:44 +0000 Subject: [PATCH 2/3] [autofix.ci] apply automated fixes --- packages/nuxt/test/page-metadata.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/test/page-metadata.test.ts b/packages/nuxt/test/page-metadata.test.ts index ab1c44b46a..1659964382 100644 --- a/packages/nuxt/test/page-metadata.test.ts +++ b/packages/nuxt/test/page-metadata.test.ts @@ -99,9 +99,11 @@ definePageMeta({ name: 'bar' }) "meta": { "__nuxt_dynamic_meta_key": Set { "props", + "middleware", "meta", }, }, + "middleware": [], "name": "some-custom-name", "path": "/some-custom-path", "props": { @@ -137,9 +139,11 @@ definePageMeta({ name: 'bar' }) { "meta": { "__nuxt_dynamic_meta_key": Set { + "middleware", "meta", }, }, + "middleware": [], "name": "some-custom-name", "path": "/some-custom-path", } @@ -165,7 +169,7 @@ definePageMeta({ name: 'bar' }) { "meta": { "__nuxt_dynamic_meta_key": Set { - "meta", + "middleware", }, }, "name": "some-custom-name", From c96bf6048e6bbcea64eaf466bfa85bc077341dad Mon Sep 17 00:00:00 2001 From: Saeid Zareie <65568529+Saeid-Za@users.noreply.github.com> Date: Thu, 6 Feb 2025 23:55:59 +0330 Subject: [PATCH 3/3] fix: adding meta to middleware key --- packages/nuxt/src/pages/utils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 71833d881d..f9ca24cc7d 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -255,6 +255,10 @@ export async function getRouteMeta (contents: string, absolutePath: string, extr if (!property) { continue } const propertyValue = withLocations(property.value) + + if (key === 'middleware') { + dynamicProperties.add('meta') + } if (propertyValue.type === 'ObjectExpression') { const valueString = js.code.slice(propertyValue.start, propertyValue.end)