mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
fix(nuxt): merge route meta properties with scanned meta (#28170)
This commit is contained in:
parent
eb31abe10e
commit
242b4710ce
@ -144,7 +144,12 @@ export async function augmentPages (routes: NuxtPage[], vfs: Record<string, stri
|
||||
for (const route of routes) {
|
||||
if (route.file && !augmentedPages.has(route.file)) {
|
||||
const fileContent = route.file in vfs ? vfs[route.file] : fs.readFileSync(await resolvePath(route.file), 'utf-8')
|
||||
Object.assign(route, await getRouteMeta(fileContent, route.file))
|
||||
const routeMeta = await getRouteMeta(fileContent, route.file)
|
||||
if (route.meta) {
|
||||
routeMeta.meta = { ...routeMeta.meta, ...route.meta }
|
||||
}
|
||||
|
||||
Object.assign(route, routeMeta)
|
||||
augmentedPages.add(route.file)
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,16 @@
|
||||
"path": ""/"",
|
||||
},
|
||||
],
|
||||
"route.meta generated 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 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 || []",
|
||||
|
@ -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)",
|
||||
|
@ -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<string, any> }>
|
||||
output?: NuxtPage[]
|
||||
normalized?: Record<string, any>[]
|
||||
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: `
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
hello: 'world'
|
||||
})
|
||||
</script>
|
||||
`,
|
||||
},
|
||||
],
|
||||
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<string, any> = {}
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user