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 1e67ba29b1..67484d4703 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 bb5d516123..6b51be5fe0 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 = {} @@ -254,6 +254,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) diff --git a/packages/nuxt/test/page-metadata.test.ts b/packages/nuxt/test/page-metadata.test.ts index d1420593cd..c07639e046 100644 --- a/packages/nuxt/test/page-metadata.test.ts +++ b/packages/nuxt/test/page-metadata.test.ts @@ -124,9 +124,11 @@ definePageMeta({ name: 'bar' }) "meta": { "__nuxt_dynamic_meta_key": Set { "props", + "middleware", "meta", }, }, + "middleware": [], "name": "some-custom-name", "path": "/some-custom-path", "props": { @@ -162,9 +164,11 @@ definePageMeta({ name: 'bar' }) { "meta": { "__nuxt_dynamic_meta_key": Set { + "middleware", "meta", }, }, + "middleware": [], "name": "some-custom-name", "path": "/some-custom-path", } @@ -190,7 +194,7 @@ definePageMeta({ name: 'bar' }) { "meta": { "__nuxt_dynamic_meta_key": Set { - "meta", + "middleware", }, }, "name": "some-custom-name", diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index d7fa131968..d81b0a9635 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -325,7 +325,7 @@ export default defineResolvers({ 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. *