fix(nuxt): extract all-literal page meta (#27821)

This commit is contained in:
Alexander Lichter 2024-06-25 18:53:30 +02:00 committed by GitHub
parent 9d3b43a6c8
commit d23c79f395
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 7 deletions

View File

@ -256,13 +256,19 @@ export async function getRouteMeta (contents: string, absolutePath: string): Pro
extractedMeta[key] = property.value.value
}
const extraneousMetaKeys = pageMetaArgument.properties
.filter(property => property.type === 'Property' && property.key.type === 'Identifier' && !(extractionKeys as unknown as string[]).includes(property.key.name))
// @ts-expect-error inferred types have been filtered out
.map(property => property.key.name)
if (extraneousMetaKeys.length) {
for (const property of pageMetaArgument.properties) {
if (property.type !== 'Property') {
continue
}
const isIdentifierOrLiteral = property.key.type === 'Literal' || property.key.type === 'Identifier'
if (!isIdentifierOrLiteral) {
continue
}
const name = property.key.type === 'Identifier' ? property.key.name : String(property.value)
if (!(extractionKeys as unknown as string[]).includes(name)) {
dynamicProperties.add('meta')
break
}
}
if (dynamicProperties.size) {

View File

@ -75,6 +75,28 @@ describe('page metadata', () => {
}
`)
})
it('should extract serialisable metadata all quoted', async () => {
const meta = await getRouteMeta(`
<script setup>
definePageMeta({
"otherValue": {
foo: 'bar',
},
})
</script>
`, filePath)
expect(meta).toMatchInlineSnapshot(`
{
"meta": {
"__nuxt_dynamic_meta_key": Set {
"meta",
},
},
}
`)
})
})
describe('normalizeRoutes', () => {