diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 66997c121c..c90e890883 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -144,7 +144,12 @@ export async function augmentPages (routes: NuxtPage[], vfs: Record import("pages/page-with-meta.vue").then(m => m.default || m)", + "meta": "{ ...(mockMeta || {}), ...{"test":1} }", + "name": "mockMeta?.name ?? "page-with-meta"", + "path": "mockMeta?.path ?? "/page-with-meta"", + "redirect": "mockMeta?.redirect", + }, + ], "should allow pages with `:` in their path": [ { "alias": "mockMeta?.alias || []", @@ -349,6 +359,16 @@ "redirect": "mockMeta?.redirect", }, ], + "should merge route.meta with meta from file": [ + { + "alias": "mockMeta?.alias || []", + "component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)", + "meta": "{ ...(mockMeta || {}), ...{"test":1} }", + "name": "mockMeta?.name ?? "page-with-meta"", + "path": "mockMeta?.path ?? "/page-with-meta"", + "redirect": "mockMeta?.redirect", + }, + ], "should not generate colliding route names when hyphens are in file name": [ { "alias": "mockMeta?.alias || []", 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 9fb308e7c1..42c8e219f6 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 @@ -16,6 +16,14 @@ "path": ""/"", }, ], + "route.meta generated from file": [ + { + "component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)", + "meta": "{"test":1}", + "name": ""page-with-meta"", + "path": ""/page-with-meta"", + }, + ], "should allow pages with `:` in their path": [ { "component": "() => import("pages/test:name.vue").then(m => m.default || m)", @@ -240,6 +248,14 @@ "path": ""/"", }, ], + "should merge route.meta with meta from file": [ + { + "component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)", + "meta": "{ ...(mockMeta || {}), ...{"test":1} }", + "name": ""page-with-meta"", + "path": ""/page-with-meta"", + }, + ], "should not generate colliding route names when hyphens are in file name": [ { "component": "() => import("pages/parent/[child].vue").then(m => m.default || m)", diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index 3babb4cf11..9bd89de9ad 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -20,7 +20,7 @@ describe('pages:generateRoutesFromFiles', () => { const tests: Array<{ description: string - files?: Array<{ path: string, template?: string }> + files?: Array<{ path: string, template?: string, meta?: Record }> output?: NuxtPage[] normalized?: Record[] error?: string @@ -554,6 +554,53 @@ describe('pages:generateRoutesFromFiles', () => { }, ], }, + { + description: 'route.meta generated from file', + files: [ + { + path: `${pagesDir}/page-with-meta.vue`, + meta: { + test: 1, + }, + }, + ], + output: [ + { + name: 'page-with-meta', + path: '/page-with-meta', + file: `${pagesDir}/page-with-meta.vue`, + children: [], + meta: { test: 1 }, + }, + ], + }, + { + description: 'should merge route.meta with meta from file', + files: [ + { + path: `${pagesDir}/page-with-meta.vue`, + meta: { + test: 1, + }, + template: ` + + `, + }, + ], + output: [ + { + name: 'page-with-meta', + path: '/page-with-meta', + file: `${pagesDir}/page-with-meta.vue`, + children: [], + meta: { [DYNAMIC_META_KEY]: new Set(['meta']), test: 1 }, + }, + ], + }, ] const normalizedResults: Record = {} @@ -572,7 +619,13 @@ describe('pages:generateRoutesFromFiles', () => { shouldUseServerComponents: true, absolutePath: file.path, relativePath: file.path.replace(/^(pages|layer\/pages)\//, ''), - }))) + }))).map((route, index) => { + return { + ...route, + meta: test.files![index].meta, + } + }) + await augmentPages(result, vfs) } catch (error: any) { expect(error.message).toEqual(test.error)