This commit is contained in:
Saeid Zareie 2025-02-18 11:13:51 +01:00 committed by GitHub
commit fd25b58e8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 4 deletions

View File

@ -332,7 +332,7 @@ globalThis.Buffer = globalThis.Buffer || Buffer
## scanPageMeta ## 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. 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.

View File

@ -202,7 +202,7 @@ export function extractScriptContent (sfc: string) {
} }
const PAGE_META_RE = /definePageMeta\([\s\S]*?\)/ 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 DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const
const pageContentsCache: Record<string, string> = {} const pageContentsCache: Record<string, string> = {}
@ -255,6 +255,10 @@ export async function getRouteMeta (contents: string, absolutePath: string, extr
const propertyValue = withLocations(property.value) const propertyValue = withLocations(property.value)
if (key === 'middleware') {
dynamicProperties.add('meta')
}
if (propertyValue.type === 'ObjectExpression') { if (propertyValue.type === 'ObjectExpression') {
const valueString = js.code.slice(propertyValue.start, propertyValue.end) const valueString = js.code.slice(propertyValue.start, propertyValue.end)
try { try {

View File

@ -124,9 +124,11 @@ definePageMeta({ name: 'bar' })
"meta": { "meta": {
"__nuxt_dynamic_meta_key": Set { "__nuxt_dynamic_meta_key": Set {
"props", "props",
"middleware",
"meta", "meta",
}, },
}, },
"middleware": [],
"name": "some-custom-name", "name": "some-custom-name",
"path": "/some-custom-path", "path": "/some-custom-path",
"props": { "props": {
@ -162,9 +164,11 @@ definePageMeta({ name: 'bar' })
{ {
"meta": { "meta": {
"__nuxt_dynamic_meta_key": Set { "__nuxt_dynamic_meta_key": Set {
"middleware",
"meta", "meta",
}, },
}, },
"middleware": [],
"name": "some-custom-name", "name": "some-custom-name",
"path": "/some-custom-path", "path": "/some-custom-path",
} }
@ -190,7 +194,7 @@ definePageMeta({ name: 'bar' })
{ {
"meta": { "meta": {
"__nuxt_dynamic_meta_key": Set { "__nuxt_dynamic_meta_key": Set {
"meta", "middleware",
}, },
}, },
"name": "some-custom-name", "name": "some-custom-name",

View File

@ -325,7 +325,7 @@ export default defineResolvers({
inlineRouteRules: false, 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. * This only works with static or strings/arrays rather than variables or conditional assignment.
* *

View File

@ -34,6 +34,7 @@ export type NuxtPage = {
alias?: string[] | string alias?: string[] | string
redirect?: RouteLocationRaw redirect?: RouteLocationRaw
children?: NuxtPage[] children?: NuxtPage[]
middleware?: string[] | string
/** /**
* Set the render mode. * Set the render mode.
* *