fix(nuxt): merge route meta properties with scanned meta (#28170)

This commit is contained in:
xjccc 2024-07-18 22:55:39 +08:00 committed by GitHub
parent eb31abe10e
commit 242b4710ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 97 additions and 3 deletions

View File

@ -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)
}

View 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 || []",

View File

@ -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)",

View File

@ -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)