fix(nuxt): preserve route-specific metadata on route.meta (#28441)

This commit is contained in:
xjccc 2024-08-19 20:49:22 +08:00 committed by GitHub
parent 4354d31090
commit 89ce936b45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 32 additions and 2 deletions

View File

@ -198,6 +198,31 @@ export default defineNuxtConfig({
})
```
#### Deduplication of Route Metadata
🚦 **Impact Level**: Minimal
##### What Changed
It's possible to set some route metadata using `definePageMeta`, such as the `name`, `path`, and so on. Previously these were available both on the route and on route metadata (for example, `route.name` and `route.meta.name`).
Now, they are only accessible on the route object.
##### Reasons for Change
This is a result of enabling `experimental.scanPageMeta` by default, and is a performance optimization.
##### Migration Steps
The migration should be straightforward:
```diff
const route = useRoute()
- console.log(route.meta.name)
+ console.log(route.name)
```
#### Shared Prerender Data
🚦 **Impact Level**: Medium

View File

@ -279,7 +279,7 @@ export async function getRouteMeta (contents: string, absolutePath: string): Pro
continue
}
const name = property.key.type === 'Identifier' ? property.key.name : String(property.value)
if (!(extractionKeys as unknown as string[]).includes(name)) {
if (name) {
dynamicProperties.add('meta')
break
}

View File

@ -329,6 +329,7 @@
"should properly override route name if definePageMeta name override is defined.": [
{
"component": "() => import("pages/index.vue").then(m => m.default || m)",
"meta": "mockMeta || {}",
"name": ""home"",
"path": ""/"",
},

View File

@ -15,7 +15,10 @@ describe('page metadata', () => {
for (const ext of ['js', 'jsx', 'ts', 'tsx', 'mjs', 'cjs']) {
const meta = await getRouteMeta(fileContents, `/app/pages/index.${ext}`)
expect(meta).toStrictEqual({
name: 'bar',
'name': 'bar',
'meta': {
'__nuxt_dynamic_meta_key': new Set(['meta']),
},
})
}
})

View File

@ -348,6 +348,7 @@ describe('pages:generateRoutesFromFiles', () => {
name: 'home',
path: '/',
file: `${pagesDir}/index.vue`,
meta: { [DYNAMIC_META_KEY]: new Set(['meta']) },
children: [],
},
],