From 89ce936b4517f9639b1202377d01eec76eda4a9d Mon Sep 17 00:00:00 2001 From: xjccc <546534045@qq.com> Date: Mon, 19 Aug 2024 20:49:22 +0800 Subject: [PATCH] fix(nuxt): preserve route-specific metadata on `route.meta` (#28441) --- docs/1.getting-started/12.upgrade.md | 25 +++++++++++++++++++ packages/nuxt/src/pages/utils.ts | 2 +- .../pages-override-meta-enabled.test.ts.snap | 1 + packages/nuxt/test/page-metadata.test.ts | 5 +++- packages/nuxt/test/pages.test.ts | 1 + 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/1.getting-started/12.upgrade.md b/docs/1.getting-started/12.upgrade.md index 0d16b09733..6ba57a3c54 100644 --- a/docs/1.getting-started/12.upgrade.md +++ b/docs/1.getting-started/12.upgrade.md @@ -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 diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 08c86afeaa..cdd89634a6 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -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 } diff --git a/packages/nuxt/test/__snapshots__/pages-override-meta-enabled.test.ts.snap b/packages/nuxt/test/__snapshots__/pages-override-meta-enabled.test.ts.snap index 861a18f16f..9dd9aaca80 100644 --- a/packages/nuxt/test/__snapshots__/pages-override-meta-enabled.test.ts.snap +++ b/packages/nuxt/test/__snapshots__/pages-override-meta-enabled.test.ts.snap @@ -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": ""/"", }, diff --git a/packages/nuxt/test/page-metadata.test.ts b/packages/nuxt/test/page-metadata.test.ts index 337871af5f..d1815a2847 100644 --- a/packages/nuxt/test/page-metadata.test.ts +++ b/packages/nuxt/test/page-metadata.test.ts @@ -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']), + }, }) } }) diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index 3b307387be..b175fd10ff 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -348,6 +348,7 @@ describe('pages:generateRoutesFromFiles', () => { name: 'home', path: '/', file: `${pagesDir}/index.vue`, + meta: { [DYNAMIC_META_KEY]: new Set(['meta']) }, children: [], }, ],