Skip to content

Commit

Permalink
fix(nuxt): merge route meta properties with scanned meta (nuxt#28170)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjccc authored Jul 18, 2024
1 parent eb31abe commit 242b471
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/nuxt/src/pages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []",
Expand Down Expand Up @@ -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 || []",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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)",
Expand Down
57 changes: 55 additions & 2 deletions packages/nuxt/test/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> = {}
Expand All @@ -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)
Expand Down

0 comments on commit 242b471

Please sign in to comment.