Skip to content

Commit 11264ad

Browse files
xjcccdanielroe
authored andcommitted
fix(nuxt): merge route meta properties with scanned meta (#28170)
1 parent 2417848 commit 11264ad

4 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎packages/nuxt/src/pages/utils.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ export async function augmentPages (routes: NuxtPage[], vfs: Record<string, stri
144144
for (const route of routes) {
145145
if (route.file && !augmentedPages.has(route.file)) {
146146
const fileContent = route.file in vfs ? vfs[route.file] : fs.readFileSync(await resolvePath(route.file), 'utf-8')
147-
Object.assign(route, await getRouteMeta(fileContent, route.file))
147+
const routeMeta = await getRouteMeta(fileContent, route.file)
148+
if (route.meta) {
149+
routeMeta.meta = { ...routeMeta.meta, ...route.meta }
150+
}
151+
152+
Object.assign(route, routeMeta)
148153
augmentedPages.add(route.file)
149154
}
150155

‎packages/nuxt/test/__snapshots__/pages-override-meta-disabled.test.ts.snap‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
"path": ""/"",
1818
},
1919
],
20+
"route.meta generated from file": [
21+
{
22+
"alias": "mockMeta?.alias || []",
23+
"component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)",
24+
"meta": "{ ...(mockMeta || {}), ...{"test":1} }",
25+
"name": "mockMeta?.name ?? "page-with-meta"",
26+
"path": "mockMeta?.path ?? "/page-with-meta"",
27+
"redirect": "mockMeta?.redirect",
28+
},
29+
],
2030
"should allow pages with `:` in their path": [
2131
{
2232
"alias": "mockMeta?.alias || []",
@@ -349,6 +359,16 @@
349359
"redirect": "mockMeta?.redirect",
350360
},
351361
],
362+
"should merge route.meta with meta from file": [
363+
{
364+
"alias": "mockMeta?.alias || []",
365+
"component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)",
366+
"meta": "{ ...(mockMeta || {}), ...{"test":1} }",
367+
"name": "mockMeta?.name ?? "page-with-meta"",
368+
"path": "mockMeta?.path ?? "/page-with-meta"",
369+
"redirect": "mockMeta?.redirect",
370+
},
371+
],
352372
"should not generate colliding route names when hyphens are in file name": [
353373
{
354374
"alias": "mockMeta?.alias || []",

‎packages/nuxt/test/__snapshots__/pages-override-meta-enabled.test.ts.snap‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
"path": ""/"",
1717
},
1818
],
19+
"route.meta generated from file": [
20+
{
21+
"component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)",
22+
"meta": "{"test":1}",
23+
"name": ""page-with-meta"",
24+
"path": ""/page-with-meta"",
25+
},
26+
],
1927
"should allow pages with `:` in their path": [
2028
{
2129
"component": "() => import("pages/test:name.vue").then(m => m.default || m)",
@@ -240,6 +248,14 @@
240248
"path": ""/"",
241249
},
242250
],
251+
"should merge route.meta with meta from file": [
252+
{
253+
"component": "() => import("pages/page-with-meta.vue").then(m => m.default || m)",
254+
"meta": "{ ...(mockMeta || {}), ...{"test":1} }",
255+
"name": ""page-with-meta"",
256+
"path": ""/page-with-meta"",
257+
},
258+
],
243259
"should not generate colliding route names when hyphens are in file name": [
244260
{
245261
"component": "() => import("pages/parent/[child].vue").then(m => m.default || m)",

‎packages/nuxt/test/pages.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('pages:generateRoutesFromFiles', () => {
2020

2121
const tests: Array<{
2222
description: string
23-
files?: Array<{ path: string, template?: string }>
23+
files?: Array<{ path: string, template?: string, meta?: Record<string, any> }>
2424
output?: NuxtPage[]
2525
normalized?: Record<string, any>[]
2626
error?: string
@@ -554,6 +554,53 @@ describe('pages:generateRoutesFromFiles', () => {
554554
},
555555
],
556556
},
557+
{
558+
description: 'route.meta generated from file',
559+
files: [
560+
{
561+
path: `${pagesDir}/page-with-meta.vue`,
562+
meta: {
563+
test: 1,
564+
},
565+
},
566+
],
567+
output: [
568+
{
569+
name: 'page-with-meta',
570+
path: '/page-with-meta',
571+
file: `${pagesDir}/page-with-meta.vue`,
572+
children: [],
573+
meta: { test: 1 },
574+
},
575+
],
576+
},
577+
{
578+
description: 'should merge route.meta with meta from file',
579+
files: [
580+
{
581+
path: `${pagesDir}/page-with-meta.vue`,
582+
meta: {
583+
test: 1,
584+
},
585+
template: `
586+
<script setup lang="ts">
587+
definePageMeta({
588+
hello: 'world'
589+
})
590+
</script>
591+
`,
592+
},
593+
],
594+
output: [
595+
{
596+
name: 'page-with-meta',
597+
path: '/page-with-meta',
598+
file: `${pagesDir}/page-with-meta.vue`,
599+
children: [],
600+
meta: { [DYNAMIC_META_KEY]: new Set(['meta']), test: 1 },
601+
},
602+
],
603+
},
557604
]
558605

559606
const normalizedResults: Record<string, any> = {}
@@ -572,7 +619,13 @@ describe('pages:generateRoutesFromFiles', () => {
572619
shouldUseServerComponents: true,
573620
absolutePath: file.path,
574621
relativePath: file.path.replace(/^(pages|layer\/pages)\//, ''),
575-
})))
622+
}))).map((route, index) => {
623+
return {
624+
...route,
625+
meta: test.files![index].meta,
626+
}
627+
})
628+
576629
await augmentPages(result, vfs)
577630
} catch (error: any) {
578631
expect(error.message).toEqual(test.error)

0 commit comments

Comments
 (0)